Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Request Dispatcher

April 5, 2000

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); 

The Session Object
Building Web Applications Using Servlets and JSP
Sample Application!


Up to => Home / Authoring / Java / Servlets




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers