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

request redirect

Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

I m developing a servlet that is accessed by two sort of users let A and B be the users. Both the user belong to two different types. Both the users will send their requests to a single servlet, let's say it a mainservlet.
Now the mainservlet depending on the user type, sends the user to two different servlet. i.e. User A is sent to servlet 1 and user B is sent to servlet 2. I want the request and response objects to be accessible to servlets 1 and 2.
I m able to identify the user type. But how do i redirect the user to another servlet along with its request and response objects.
I have written the following code but its not working.
System.out.println("condition satisfied");
I get the message "condition satisfied" at the server end. But another servlet is not getting invoked.
Please suggest me how do i proceed now ?

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
That sendRedirect causes the browser to make a fresh request to the redirect URL.
If you want to send the current request to a specific servlet you need to look into the RequestDispatcher class and the forward method. As I recall, the resulting code looks something like:
RequestDispatcher rd = request.getRequestDispatcher( path )
rd.forward( request, response );
return ;
where path is relative to the application context.

author of:
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

thanx Mr. William for your reply...
I used the request dispatcher as u said. But the request and response objects are not available in the another servlet even though the forward method is executed. Let me know where I have gone wrong or shall I add some other statements to make it work.
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException{
System.out.println("In Post method"); // System.out.println to just check the execution of the program
System.out.println("condition satisfied");
RequestDispatcher rd = req.getRequestDispatcher("/anotherServlet");
System.out.println("object is null");
rd.forward(req, res);
System.out.println("its over now");
}catch(Exception e){
System.out.println("Exception occured at line 46"+e);
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
System.out.println("I m in anotherServlet now");
PrintWriter pw=res.getWriter();
pw.println("it was successful");
I m using tomcat 3.2.1 as my application server.
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

I got the reason..
that was coz of improper path. The path should look like this :
RequestDispatcher rd = req.getRequestDispatcher("/servlet/anotherServlet");
I agree. Here's the link:
subject: request redirect
It's not a secret anymore!