• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please check this thread.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic