Forums Register Login

RequestDispatcher

+Pie Number of slices to send: Send
I'm confusing with the interface RequestDispatcher.
Can you give me example so that i can understand absolute path and relative path ?
thanks
+Pie Number of slices to send: Send
ServletContext.getRequestDispatcher(pathname) The pathname must begin with a "/" and is interpreted as relative to the current context root.

ServletRequest.getRequestDispatcher(pathname) The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root.
Here with both the usages mentioned above we are using relative URLs only, with request.getRequestDispatcher we can pass the name of the resource relative to the current resource from which we are calling and with the other method we need to mention the path relative to current ServletContext and need to start the path with "/".
+Pie Number of slices to send: Send
Rao thanks for explanation.
Any example would be appreciated to be clearer for me.
+Pie Number of slices to send: Send
When using the forward method of the RequestDispatcher object to pass control from a servlet called ABCServlet to another servlet named XYZServlet, the easiest is to place the class files of both ABCServlet and XYZServlet in the same directory. This way, ABCServlet can be invoked from the URL http://domain/VirtualDir/servlet/ABCServlet and XYZServlet can be called from the URL http://domain/VirtualDir/servlet/XYZServlet. Using the forward method is then straightforward. You can use the getRequestDispatcher from the ServletRequest interface passing the name of the second servlet. In ABCServlet, you can write this code:

RequestDispatcher rd = request.getRequestDispatcher("XYZServlet"); rd.forward(request, response);
You don't need to include the / character before XYZServlet. This way is easiest because you don't need to worry about the paths of both servlets at all.
The harder way would be trying to pass this String to the getRequestDispatcher of ServletRequest:

"/servlet/XYZServlet"

If you have to invoke the forward method of a RequestDispatcher object obtained from the getRequestDispatcher of the ServletContext, you need to pass "/VirtualDir/servlet/XYZServlet" as the path argument, such as:

RequestDispatcher rd = getServletContext().getRequestDispatcher("/servlet/XYZServlet");
rd.forward(request, response);To use the getNamedDispatcher method, your code would become:
RequestDispatcher rd = getServletContext().getNamedDispatcher("XYZServlet");
rd.forward(request, response);
+Pie Number of slices to send: Send
Another sample. Say you have the FirstServlet in the directory 'classes' under your 'WEB-INF', and you want to create a RequestDispatcher to forward to SecondServlet. You could use an absolute path (e.g. "/servlet/...") as follows:
RequestDispatcher rd=getServletContext().getRequestDispatcher("/servlet/SecondServlet");
Now, say you want to forward to a jsp (e.g. Page_1_two_dirs_up.jsp) which is two directories up e.g. under your web application dir 'MyWebApp'. You could use the relative path (e.g. "../..") inside FirstServlet as follows:
RequestDispatcher rd=request.getRequestDispatcher("../Page_1_two_dirs_up.jsp");
As in previous posts, note use of request.getRequestDispatcher() and getServletContext().getRequestDispatcher().
Hope this also helps.
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4082 times.
Similar Threads
RequestDispatcher Interface.
problem with RequestDispatcher
ways to include a servlet from a JSP
getRequestDispatcher doubt
RequestDispatcher
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:35:56.