• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

difference between ways of getting RequestDispatcher

Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,
Tried googling and looking at various posts in Javaranch for the differnece between getting a RequestDispatcher from servletContext and from request .

What I understand is ::
Request :: The path can be "relative" or the path can be "literal"

ServletContext :: The Path must be literal

If in Tomcat I have the following structure ::

( JSP location )
<<TOMCAT_HOME>> / webapps / myApp / view/display.jsp

( Actual servlet location )
<<TOMCAT_HOME>> / webapps / classes / com / ex / serv1.class

In my servlet I tried both of these and it worked .

So I am still not quite sure where there would be a need to use a specific Request dispatcher ?

[ August 01, 2008: Message edited by: anagha desai ]
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the source of getRequestDispatcher in the Tomcat 5.5 implementation of HttpServletRequest

Figure it out.

Posts: 4179
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The request.getRequestDispatcher will get a dispatcher that locates content in the SAME CONTEXT as the request getting the dispather. So if you were at the URL:
and you wanted to get a URL at
Then you would use a request.getRequestDispatcher with a relative URL (not beginning with /).

If, on the other hand you wanted to get a page in the SAME CONTEXT but a different sub context, then you would still use the request.getRequestDispatcher with an absolute URL (beginning with /) which will be discovered relative to the same Context the request was made to.

In this same scenario you could use getServletContext().getRequestDispatcher and access the second URL in the same way as with request.getRequestDispatcher, so there would be no difference between a RequestDispatcher retrieved from the current servlet context and one retrieved from the request with a URL that started with "/".

But the ServletContext#getRequestDispatcher allows you to get pages from a DIFFERENT CONTEXT by using getServletContext().getContext("differntcontext").getRequestDispatcher.

You can't get a RequestDispatcher from a different Context using the request.
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic