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 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>");
}
}
Share this article :
 
Copyright © 2012. Best Online Tutorials | Source codes | Programming Languages - All Rights Reserved