• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

request redirect

 
Ranch Hand
Posts: 126
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the reason..
that was coz of improper path. The path should look like this :
RequestDispatcher rd = req.getRequestDispatcher("/servlet/anotherServlet");
nitin
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!