RequestDispatcher reqDispObj = request.getRequestDispatcher("/home.jsp");
RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/ContextRoot/home.jsp");
Comments are closed for "Difference between ServletRequest.getRequestDispatcher and ServletContext.getRequestDispatcher".
servletcontext.RequestDispatcher() it locates the resource relative to the context path
servletrequest.RequestDispatcher(): it locates the resource relative to the request path
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
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
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 :-) …
Thanks Joe…
Your blog is best place to get some irritating question cleared !
release a ebook based on the blog !would really rock !
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.
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.
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.
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”);
thank you for tutorial…
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.
No matter who clears my queries your blog is best place to be around :)