wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ***Difference b/w getRequestDispatcher() for ServletContext and ServletRequest***  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "***Difference b/w getRequestDispatcher() for ServletContext and ServletRequest***  " Watch "***Difference b/w getRequestDispatcher() for ServletContext and ServletRequest***  " New topic
Author

***Difference b/w getRequestDispatcher() for ServletContext and ServletRequest***

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Hi all,

What is the difference b/w ServletContext.getRequestDispatcher(String path) and
ServletRequest.getRequestDispatcher(String path) ?

Where the API doc says:
For ServletRequest.getRequestDispatcher(String path)
"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. ...."


and for ServletContext.getRequestDispatcher(String path)
"The pathname must begin with a "/" and is interpreted as relative to the current context root.............."


Both look kinda similar to me....can anyone expalin the differnce with an EXAMPLE??

Regards,
Amit
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
hi amit das...
there is the difference b/w the both....we can pass a relative path to the ServletRequest.getRequestDispatcher() method but not to the getRequestDispatcher() method of ServletContext.

ServletRequest.getRequestDispatcher() method will evaluate the path relative to the path of the request but for the method of ServletContext, the path parameter cannot be relative and must start with /( / means at the root of the web application not current relative path) . As the Servlet Request has a current request path to evaluate the relative path while ServletContext does not.

Hope it helps
vandana
Vijay P
Greenhorn

Joined: Apr 03, 2000
Posts: 8
A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. So once you get the RequestDispatcher, you could invoke include() or forward() function.

ServletContext.getRequestDispatcher(String path) requires the resource path to begin with "/", i.e. a path relative to context of your application. Lets say your application context path is /myapp, when a web request with any URL (localhost:8080/myapp/arbitrarydir1/dir2/myresource) path that invokes the servlet, doing ServletContext.getRequestDispatcher("/page2.jsp") will expect the file "page2.jsp" to be in directory /myapp/page2.jsp


Where as ServletRequest.getRequestDispatcher(String path) take the path relative to the path of resource being requested. So doing a ServletRequest.getRequestDispatcher("page2.jsp") while processing a request for http://localhost:8080/myapp/module1/myresource, will look for page2.jsp under directory /myapp/module1/

and doing a ServletRequest.getRequestDispatcher("page2.jsp") while processing a request for http://localhost:8080/myapp/moduleX/myresource, will look for page2.jsp under the following directory /myapp/moduleX/


-Vj
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Also, please check this thread.
Surinder Singh
Greenhorn

Joined: May 04, 2006
Posts: 1
you can pass a relative path to the getRequestDispatcher() method of ServletRequest but not to the getRequestDispatcher() method of ServletContext .

For example,
request.getRequestDispatcher("../html/copyright.html") is valid,and the getRequestDispatcher() method of ServletRequest will evaluate the path relative to the path of the request.

For the getRequestDispatcher()method of ServletContext, the path parameter cannot be relative and must start with /. This makes sense because ServletRequest has a current request path to evaluate the relative path while ServletContext does not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ***Difference b/w getRequestDispatcher() for ServletContext and ServletRequest***