Tutorials : Building Web Applications Using Servlets and JSP :

Request Dispatcher

The basic use of a RequestDispatcher class is to 'pass on' the current request to another program(servlet) and hence allow 'chaining' of the programs. A RequestDispatcher has two primary methods for 'including' the response of another program or 'forwarding' the request of the current program to another one.

One can effectively use the RequestDispatcher to call a JSP from a servlet or a servlet from another servlet. Again we will use the Session and RequestDispatcher in our sample web application. Let's first understand the basic concepts.

So to get the RequestDispatcher object:

	
RequestDispatcher rd = 
  getServletContext().getRequestDispatcher("/welcome.jsp");

We have not covered the ServletContext class but at this point it will suffice to know that it holds the current servlet's context information.

So the above line will get a RequestDispatcher for a file welcome.jsp.

The getRequestDispatcher method will return null if it cannot find the specified resource (welcome.jsp in our case) or it cannot produce a dispatcher for specified resource. For e.g. if you want a dispatcher for a text file and your servlet engine does not support that, then you will get a null RequestDispatcher if you write:

	
RequestDispatcher rd = 
  getServletContext().getRequestDispatcher("/welcome.txt");

You can get a dispatcher for a servlet by:

	
RequestDispatcher rd = 
  getServletContext().getRequestDispatcher("/servlet/Welcome");

The forward method of the request dispatcher forwards the current request to the specified resource. Let's say we are writing dispatcherTest servlet :

public class DispatcherTest extends HttpServlet {
	public void doGet(HttpServletRequest request,
                HttpServletResponse response)
	        throws ServletException, IOException {

And after some processing is done we want to forward the request to welcome.jsp.

We first get a request dispatcher:

	
RequestDispatcher rd = 
  getServletContext().getRequestDispatcher("/welcome.jsp");

Then we forward the request by:

	
rd.forward(request, response);

This will 'execute' the welcome.jsp , pass current request object to it and send its response to the client. Any code written in our DispatcherTest servlet after "rd.forward(request, response);" will not execute as the request is already forwarded.

Think of this as 'calling' welcome.jsp from DispatcherTest servlet. But the welcome.jsp function call will not reurn back to the DispatcherTest servlet , instead it will return its response to the client.

Now what if we wanted the control to be returned in the DispatcherTest servlet? Let's say we want to 'call' another servlet from DispatcherTest servlet and we want to process some more stuff further after calling this new servlet.

Yes, we have a way! We can use the include method. The syntax is:

	
rd.include(request, response); 

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.