File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic


Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
I'm confusing with the interface RequestDispatcher.
Can you give me example so that i can understand absolute path and relative path ?
Rao Kodeboyina
Ranch Hand

Joined: Jun 09, 2002
Posts: 35
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 "/".

Sun Certified Web Components Developer for J2EE<br />Sun Certified Programmer for Java™ 2 Platform
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Rao thanks for explanation.
Any example would be appreciated to be clearer for me.
Rao Kodeboyina
Ranch Hand

Joined: Jun 09, 2002
Posts: 35
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:


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);
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
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.
I agree. Here's the link:
subject: RequestDispatcher
It's not a secret anymore!