File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Amit Das
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please check this thread.
 
Surinder Singh
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic