Request dispatching in Servlets

When building a Web application, it is often useful to forward the processing of a request to another servlet, or to include the output of ...


When building a Web application, it is often useful to forward the processing of a request to another servlet, or to include the output of another servlet in the response. The RequestDispatcher interface provides a mechanism to accomplish this, by defining a request dispatcher object that receives a request from the client and sends it to any resource to be further processed.

Similar to response redirection, this resource may be another active server resource, such as a servlet or JSP file, and the resource (URL) must be available to the calling servlet, in the same servlet context. Unlike response redirection, however, the request object is available to the called resource, in other words, it remains in scope.

We define an active application resource as one which can handle the request, such as another servlet or a JSP file (something in this case which has access to the request object). A passive resource would be an HTML file, which cannot explicitly handle the request, but is available to the application, usually in the document root.

An object implementing the RequestDispatcher interface may be obtained by the ServletContext through the getRequestDispatcher method. This method takes a String argument describing a path within the scope of the ServletContext. The path must be relative to the root of the ServletContext. 

There are two ways to use a request dispatcher object:

RequestDispatcher.forward: Forwards the responsibility of processing the request and creating the response to another active resource. It is illegal to use this method if a reference to the PrintWriter output object has already been made (which is responsible for sending the response). HTMLFormHandlerDispatcher1 (Code A) calls DispatcherForward (Code B) using the forward method to hand off the responsibility of processing the request and sending the response from the calling servlet to the called servlet.

RequestDispatcher.include: The include method of the RequestDispatcher interface provides the calling servlet the ability to respond to the client, but to use the included object’s resource for part of the reply. Here, it can have the PrintWriter output object open, because the calling servlet is still responsible for handling the request. The called resource, however, cannot set headers in the client response. HTMLFormHandlerDispatcher2 (Code C) calls DispatcherInclude (Code D) where we are using these two servlets together to produce a single response. Control is returned to the calling servlet.

In both the forward and include methods, the request object remains in the scope of the called object. The primary point to remember here is that when using forward, you must handle all writing of the response in the called servlet. When using include, you can write in either, or both, but you can only set the headers in the calling servlet.The HTMLFormHandlerDispatcherX servlets are invoked from an HTML form generated by appropriate HTMLFormGeneratorDisplatcherX servlets.Figure 65. Request dispatching servlet: calling servlet through forward method Figure 66. Request dispatching servlet: called servlet through forward method

CODE A

package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTMLFormHandlerDispatcher1 extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
RequestDispatcher rd = getServletContext().getRequestDispatcher
("/servlet/itso.servjsp.servletapi.DispatcherForward");
rd.forward(req, res);
}
}

CODE B

package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DispatcherForward extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><BODY>Start of FORWARDED request");
out.println("<P>Hi " + req.getParameter("firstname"));
out.println("<BR>I see you are a " + req.getParameter("title"));
out.println("<P>End of request</BODY></HTML>");
}
}

CODE C

package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTMLFormHandlerDispatcher2 extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><BODY>Start of INCLUDED request");
out.println("<P>Hi " + req.getParameter("firstname"));
out.flush();
RequestDispatcher rd = getServletContext().getRequestDispatcher
("/servlet/itso.servjsp.servletapi.DispatcherInclude");
rd.include(req, res);
out.println("<P>End of request</BODY></HTML>");
}
}

Note: You must flush the output before including the servlet, otherwise the output may be in the wrong order.

CODE D

package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DispatcherInclude extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("<HR>I see you are a " + req.getParameter("title"));
out.println("<P>End of include<HR>");
}
}
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Request dispatching in Servlets
Request dispatching in Servlets
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/request-dispatching-in-servlets.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/request-dispatching-in-servlets.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content