servlet页面跳转多次传参

时间:2022-12-13 21:10:11

servlet如何从表单中获取,并连续多次跳转如何,再次获取表单参数
 第一种方法:
(第一个Serverlet类   注意:本章未进行乱码处理,不能传汉字)
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


String name=request.getParameter("name");
String password=request.getParameter("password");

if(name.equals("456") && password.equals("123")){
ServletContext context=this.getServletContext();
context.setAttribute("name", name);
response.sendRedirect("successful");
}
}
(第二个Serverlet类---》也就是第一个Serverlet类挑转到successful.java )
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {             ServletContext context=this.getServletContext();System.out.println(context.getAttribute("name"));}
第二种(较为简单,使用forward()注意:本章未进行乱码处理,不能传汉字)
类1
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


String name=request.getParameter("name");
String password=request.getParameter("password");
request.getRequestDispatcher("successful").forward(request, response);
}类2
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("传参成功!!!");System.out.println(request.getParameter("name"));}