JavaResp JavaResp - 4 months ago 23x
Java Question

ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()


getRequestDispatcher(String path) of
the ServletRequest interface cannot
extend outside the current servlet

where as

getRequestDispatcher(String path) of
the ServletContext can use the
getContext(String uripath) method to
obtain RequestDispatcher for resources
in foreign contexts.

and how??

Please help


If you use an absolute path such as ("/index.jsp"), there is no difference.

If you use relative path, you must use HttpServletRequest.getRequestDispatcher(). ServletContext.getRequestDispatcher() doesn't allow it.

For example, if you receive your request on,

    RequestDispatcher dispatcher = 
    dispatcher.forward( request, response ); 

Will forward the request to the page

In any case, you can't forward request to a resource outside of the context.