1,重定向
response.sendRedirect(java.lang.String location);
原理:
重定向与客户端交互,服务器将传递进来的location直接相应到客户端,会产生302码,浏览器自动访问新的location。
特点:
两次请求和相应
URL地址发生了变化
数据会丢失,产生了两个不同的HttpServletRequest对象
地址问题:
重定向有绝对地址和相对地址之分
绝对地址不加“/”
response.sendRedirect("second.do");
相对地址加“/”
response.sendRedirect(request.getContextPath()+"/second.do");
2,请求分派
RequestDispatcher rd=request.getRequestDispatcher("second.do");
rd.forward(request, response);
原理:
发生在服务器端,一个servlet对象调用另外一个servlet对象,会将request和response传递过去;
特点:
只有一次请求和响应
URL地址不会改变
数据不会丢失