RequestDispatcher Interface |
public interface RequestDispatcher |
A RequestDispatcher is an object that sends requests from client machines to the appropriate resource (servlet, HTML file, etc.) on the server. The server creates the RequestDispatcher object, which is used as a wrapper around a particular resource. A RequestDispatcher object was intended to wrap servlets, but can be used to wrap any type of resource on a server. Example: Using RequestDispatcher In this example, a RequestDispatcher object is used to include some copyright information, contained in the file copyright.html, in a response. The contents of the file are included in the response by having the RequestDispatcher object invoke the include() method.
         import javax.servlet.*;
         import java.io.*;
         public class RDServlet extends GenericServlet {
           public void service(ServletRequest request, ServletResponse response)
                   throws ServletException, IOException {
             // A RequestDispatcher object is associated with an html file
        RequestDispatcher rd =

    getServletContext().getRequestDispatcher("/copyright.html");
             // The ServletResponse object passed to the service() method is used
             // to set the content type of the response and to return a PrintWriter
             // object.  The PrintWriter writes a line of text to the client machine.
         response.setContentType("text/html");
             PrintWriter pw = response.getWriter();
             pw.println("Java Programmer's Reference");
             // The "copyright.html" file is sent to the client machine by having
             // the RequestDispatcher object invoke the include() method.  The
             // include() method is used rather than the forward() method because
             // the PrintWriter object had already been created.
             rd.include(request, response);
             pw.close();
           }
         }
The copyright.html file contains the following code:
                <BR><BR><BR>
         Copyright 2000 Wrox Press, Ltd.<BR>
         All rights reserved<BR>

forward() |
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException | Method
foward() forwards a client request to another resource (servlet, HTML file, etc.). This method allows a servlet to serve as a "request processor", performing some preliminary work before sending the request to the resource that will ultimately respond to it. The foward() method can be used if the servlet has not already opened a PrintWriter or ServletOutputStream back to the client machine. If an output stream has been created, use the include() method instead.

include() |
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException | Method
include() allows a resource to be included in the response to a client request. This method is used to include some content to the response after the response has been initiated by opening a PrintWriter or ServletOutputStream back to the client machine.