This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "request redirect" Watch "request redirect" New topic
Author

request redirect

Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

Hi,
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.
if(type.equals("USERA")){
System.out.println("condition satisfied");
res.sendRedirect("http://199.199.199.17:8080/servlet/anotherServlet");
}
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 ?

thanx
nitin
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
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.
Bill

------------------
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.
MainServlet.java
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException{
try{
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");
if(rd==null)
System.out.println("object is null");
else
rd.forward(req, res);
System.out.println("its over now");
}catch(Exception e){
System.out.println("Exception occured at line 46"+e);
}
}
===============================================================
anotherServlet.java
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");
pw.close();
}
I m using tomcat 3.2.1 as my application server.
nitin
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");
nitin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request redirect