- A process where two or more servlets communicates with each other to process the client request.
- A servlet can forward the request to another servlet to process the client request.
- A servlet can include the output of another servlet to process the client request.
- Inter-servlet communication using Request Dispatcher
- A Request Dispatcher:
- Is an object of the javax.servlet.RequestDispatcher interface that allows inter-servlet communication.
- Object is used to include the content of another servlet.
- Object is used to forward the request to another servlet.
- This interface is present in the javax.servlet package and contains only following two methods :
- forward(ServletRequest request, ServletResponse response) Forwards a request to another resource on the same server. That resource can be a Servlet, JSP page or a simple HTML page.
- include(ServletRequest request, ServletResponse response) Works like a server-side include ( SSI ) and includes the response from the given resource ( Servlet, JSP page, HTML page ) within the caller response.
- How to get a reference to RequestDispatcher Interface ?
- In order to use forward() or include() methods we discussed above we will have to get a reference to RequestDispatcher interface.
There are two ways you can do this :
- ServletContext.getRequestDispatcher(String resource)
- ServletRequest.getRequestDispatcher(String resource)
rd = request.getRequestDispatcher("inc.jsp?u1=james");
RequestDispatcher dispatcher = req.getRequestDispatcher("/index.html");
RequestDispatcher rd = request.getRequestDispatcher("/show_ctr.jsp");