17/02/25 ---------- Q)How can a servlet communicate with another servlet? =>Request Dispatching mechanism is used to implement inter-servlet communication in a Java web application. Q)How to implement RequestDispatching? Step 1:- Create RequestDispatcher object in source servlet RequestDispatcher rd=request.getRequestDispatcher("target servlet public URL name"); Step 2:- call forward/include method on the RequestDispatcher object. Note:- In forward mechanism of request dispatching, target servlet is responsible to build the response page for the client. In case of include mechanism, source servlet only is responsible. For eg. public class MyServlet1 extends GenericServlet{ public void service(ServletRequest request,ServletResponse response)throws java.io.IOException,javax.servlet.ServletException{ RequestDispatcher rd=request.getRequestDispatcher("/y"); rd.forward(request,response);//servlet 1 talking to servlet 2 } } public class MyServlet2 extends GenericServlet{ public void service(ServletRequest request,ServletResponse response)throws java.io.IOException,javax.servlet.ServletException{ } } three MyServlet1 four MyServlet2 three /x four /y Q)What are the different servlet scopes? 1)request scope 2)session scope 3)application scope Note:- whenever data sharing concept is there, we will have servlet scopes concept. =>If a data item is stored in request object, it is said to be in request scope. =>If a data item is stored in HttpSession object, that data item is said to be in session scope. =>If a data item is stored in ServletContext object, that data item is said to be in application scope. Q)How many ServletContext objects are created? =>1 per application =>As soon as the application is deployed, servlet container creates ServletContext object. =>If data is stored in ServletContext object, all the web components(servlets & jsps) of the application can share that data for the life time of the application. Q)How many request objects are created? =>For each client request, one request object is created. =>requested scoped data is shared among those servlets and jsps which are participating in request dispatching. =>request scoped data is avaiable for the duration of one request-response cycle