aspose file tools*
The moose likes Servlets and the fly likes A question on RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "A question on RequestDispatcher " Watch "A question on RequestDispatcher " New topic
Author

A question on RequestDispatcher

Johnson David
Greenhorn

Joined: May 22, 2005
Posts: 27
Hi all,

I think this is one of the common questions in servlets.

What is the difference between a RequestDispatcher object obtained from the Request object and the RequestDispatcher object obtained from the ServletContext ?


thanks in advance

Johnson
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Originally posted by johnson david:
Hi all,

I think this is one of the common questions in servlets.

What is the difference between a RequestDispatcher object obtained from the Request object and the RequestDispatcher object obtained from the ServletContext ?


thanks in advance

Johnson


Hi john,
This is from a search result i did on google for your question:

"from javadoc

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(java.lang.String path)Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.

The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.

What this means is that with the ServletContext's RequestDispatcher, the path is always set to the root of the context , ie '/' as compared to the Request's RequestDispatcher where you could specify a path relative to the resource servicing that request.

A simple example

Assume the resource that's servicing the current request is servlet/FirstServlet

Assume, i need to get to a resource /servlet/someOtherServlet using a request disptacher.


RequestDispatcher rd = getServletContext().getRequestDispatcher("/servlet/someOtherServlet");
rd.include(request, response);

Alternatively

RequestDispatcher rd = request.getRequestDispatcher("someOtherServlet"); //path relative to firstServlet
rd.include(request, response);"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: A question on RequestDispatcher