您的位置:首页 > 产品设计 > UI/UE

RequestDispaatcher 和 sendRedirect 区别

2016-02-19 00:00 465 查看
摘要: RequestDispaatcher 和 sendRedirect 区别及用法

RequestDispaatcher 在服务器端运行

如:request.RequestDispatcher 请求转发 前后页面共享同一个Request对象

sendRedirect 通过向客户端浏览器发送命令来完成

如:Response.sendRedirect 重定向 重定向后的页面创建新的Request对象

所以RequestDispatcher.forward()对于浏览器来说是“透明的”;

而HttpServletResponse.sendRedirect()则不是

RequestDispatcher 的 forward方法前必须先清空缓冲区。
"/"代表相对与web应用路径
RequestDispatcher rd = request.getRequestDispatcher("/ooo");
rd.forward(request, response);提交至
http://localhost:8080/Test/ooo
RequestDispatcher rd = getServletContext().getRequestDispatcher("/ooo");


rd.forward(request, response);提交至http://localhost:8080/Test/ooo

RequestDispatcher rd =getServletContext().getNamedDispatcher("TestServlet");

(TestServlet为一个 <servlet-name>)

rd.forward(request, response);

提交至名为TestServlet的servlet
如果在<jsp:forward>之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用。
另外要注意:它不能改变浏览器地址,刷新的话会导致重复提交
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息