How to access only Servlet2 if path is /Servlet1/Servlet2
Suppose I want to have path look like this /Servlet1?id=34/Servlet2
I want to access Servlet2 depending on the id parameter received in Servlet1, but when I try to do so system access service methods of Servlet1 only and do not go to Servlet2
I just want to skip the code for Servlet1 and move to Servlet2 when my path is /Servlet1?id=34/Servlet2 otherwise run code in Servlet 1 when path is /Servlet1?id=34
I am not sure when such approach is to be used, where you access a Servlet which is like a descendent to another servlet. If I am taking any concept wrong kindly correct or else suggest some solution to make it work
EDIT: If user enters /Servlet1?id=34 then it shows an entirely different page and when user enters /Servlet1?id=34/Servlet2 I want to show a different page based on the id specified in Servlet1.
Problem is that the URI that I get in Servlet1 when user enter path /Servlet1?id=34/Servlet2 is only /Servlet1 ie Servlet2 is nowhere in URI so how do I identify which Servlet user is looking for?
Use RequestDispatcher("servlet2")
's forward(req,res)
method from Servlet1
to forward the control to Sertvlet2
.
Edit:
public class Servlet1 extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id= request.getParameter("id");
/*
* You can do any processing here.
* We will simply output the value of name parameter on server console.
*
*/
if(id.equals("32")){
RequestDispatcher rd =
getServletContext().getRequestDispatcher("Servlet2");
rd.forward(request, response);
}
}
}
链接地址: http://www.djcxy.com/p/76554.html