Servlet中,重定向和请求分派的区别!!!

来源:互联网 发布:windows ntp服务器软件 编辑:程序博客网 时间:2024/05/28 17:08

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地址不会改变

数据不会丢失