Difference between ServletRequest.getRequestDispatcher and ServletContext.getRequestDispatcher

06/05/2008
  • request.getRequestDispatcher(“url”) means the dispatch is relative to the current HTTP request.
    Example code: RequestDispatcher reqDispObj = request.getRequestDispatcher("/home.jsp");
  • getServletContext().getRequestDispatcher(“url”) means the dispatch is relative to the root of the ServletContext.
    Example code:RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/ContextRoot/home.jsp");

a didn’t understand anything

mario on November 23rd, 2010 1:08 am

Please make it more clear…I cant understand here what it means…thnx

Deepak on January 11th, 2011 12:52 pm

servletcontext.ReuestDispatcher() it locates the resouce relative to the context path
servletrequest.ReuestDispatcher(): it locates the resource relative to the request path

Anonymous on May 9th, 2011 9:25 am

request.getRequestDispatcher(“url”) is for chaing to servlets
and servletContext.getRequestDispacher(“destination url”) is for chainig two web applications with in the same server/two different servers

ashok on August 3rd, 2011 10:36 am

request.getRequestDispatcher(“url”) is for chaing two servlets with in the same web application and servletContext.getRequestDispacher(“destination url”) is for chainig two web applications with in the same server/two different servers

ashok on August 3rd, 2011 10:38 am

Hi,

Can you please explain this, by giving some example. I really did not get anything. :(

Shitansu on October 19th, 2011 8:27 pm

Oh Yes,

Now I got it. The difference is only the PATH.

ServletRequest.getRequestDispatcher() is taking the relative path of the url as argument
(Ex- /home.jsp)

ServletContext.getRequestDispatcher() is taking the absolute path of the url as argument.
(Ex- /MyPackage/home.jsp)

Nice :-) …

Shitansu on October 19th, 2011 8:35 pm

Thanks Joe…
Your blog is best place to get some irritating question cleared !
release a ebook based on the blog !would really rock !

KarpovMohan on March 6th, 2012 12:09 am

Can you have a look at this page. Text are overriden

Anonymous on March 9th, 2012 4:09 pm

I am not getting how to give another context path to getServletContext().getRequestDispatcher(path).

firstly can we call forward on another application.if i have two applications i want to call resource of one application to another application.

Anonymous on March 12th, 2012 9:35 pm

Its not clear

alwar on March 18th, 2012 8:24 am

The difference between ServletRequest.getRequestDispatcher(String path) and ServletContext.getRequestDispatcher(String path) is that the former can accept a relative path as well whereas the latter can accept paths relative to the current context root only.

If the path starts with a ‘/’ in the getRequestDispatcher(String path) of the ServletRequest interface then it’s interpreted as being relative to the current context root otherwise it’ll be a relative to the request of the calling servlet. Whereas, the path of the getRequestDispatcher(String path) of the ServletContext interface must start with ‘/’ only – being relative to the current context root.

Another difference between the two is that path of the getRequestDispatcher(String path) of the ServletRequest interface cannot extend outside the current servlet context whereas getRequestDispatcher(String path) of the ServletContext can use the getContext(String uripath) method to obtain RequestDispatcher for resources in foreign contexts.

Sangeetha on March 20th, 2012 9:36 pm

Hi Joe,

A lot of Title content here is overlapped with explanation which makes it difficult to read. Can you please do something with it.

ankur on May 13th, 2012 4:34 pm

@Ankur, yes thats bad, I will fix the alignment quickly.

Joe on May 13th, 2012 11:45 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home