Difference between ServletRequest.getRequestDispatcher and ServletContext.getRequestDispatcher

This Servlet tutorial was added on 06/05/2008.

Comments on "Difference between ServletRequest.getRequestDispatcher and ServletContext.getRequestDispatcher" Tutorial:

  1. Anonymous says:

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

  2. ashok says:

    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

  3. ashok says:

    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

  4. Shitansu says:

    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 :-) …

  5. KarpovMohan says:

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

  6. Anonymous says:

    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.

  7. Sangeetha says:

    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.

  8. Kapil says:

    Just correct following if it is wrong.
    ServletContect.getRequseDispature(): means we call any jsp or servlet from one server to other.
    ServletRequset.getRequseDispature(): means we call any servlet or jsp withing the current application, not in other server or application.

    Is it write.

  9. sudip biswas says:

    clear view of Above code of joe:
    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”);

  10. sudip biswas says:

    thank you for tutorial…

  11. suresh.A says:

    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 getRequestDispatche(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.

  12. Yogesh R K says:

    No matter who clears my queries your blog is best place to be around :)

Comments are closed for this "Difference between ServletRequest.getRequestDispatcher and ServletContext.getRequestDispatcher" tutorial.