总结一:
重定向:是服务器对客户端做出的响应,让客户端重新请求request面
转 发:转发是服务器去自己去请求新的页面,而不做出响应,做出响应应该是新页面的事情
总结二
什么时候用重定向(sendRedirect),什么时候用转发(forward),什么时候用包含(include)
重定向:会重新请求一次,所以地址栏会显示重新请求的地址,而且
request和response都会重新生成
转 发:只是服务器内转发,地址栏还是i显示请求的地址,并且requset和response数据还在,但是体内信息不存在了,也就是响应数据
包 含:和转发一样,但是响应数据还在
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("ouyang")){
HttpSession httpSession = request.getSession();
httpSession.setAttribute("username",username);
response.sendRedirect("/welcome.jsp");
}else{
request.setAttribute("warn","登陆失败,请重新登陆");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
response.getWriter().println("123");
}