页面跳转问题
- 服务器端跳转(转发)forword
特点:
1、只会发生一次请求
2、地址栏不会发生跳转
req.getRequestDispatcher("success.html").forward(req, resp);
- 客户端跳转(重定向)redirect
1、不仅仅会发生一次请求,至少两次
2、地址栏会发生变化
resp.sendRedirect("error.html");
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收参数
resp.setContentType("text/html;charset=utf-8");
String name=req.getParameter("name");
String password=req.getParameter("password");
// System.out.println(name+age);
if(name!=null&&name.trim().equals("lilei")&&password!=null&&password.trim().equals("123")) {
//登录成功
System.out.println("登录成功");
//使用转发(服务器端跳转)forword
/*
* RequestDispatcher dispatcher= req.getRequestDispatcher("success.html");
* dispatcher.forward(req, resp);
*/
//推荐写法
req.getRequestDispatcher("success.html").forward(req, resp);
}else {
System.out.println("登录失败");
resp.sendRedirect("error.html");
}
}