sendRedirect 与forward的区别

1.在JSP中要跳转到index.jsp页面:
<jsp:forward page="index.jsp"/>
2.在Servlet中要跳转到index.jsp页面:
forward(response);
sendRedirect:
1.sendRedirect方式跳转到index.jsp页面:
response.sendRedirect("index.jsp");
两者的区别如下:
1.请求次数不同,这是最本质的区别。在request 2.传值方式不同。在request对象,所以可以使用redirect方式下,当前文件和目标文件属于不同的请求,每次请求会单独创建response对象,这样就不能使用request中,然后request中获取需要的信息。如果使用sendRedirect方式在控制器和视图之间传递信息,需要使用在目标文件之后加上“?名字=值”的方式传递。
3.客户端在地址栏中看到的地址不一样,对于forward方式,地址栏中是index.jsp,这样系统会在First文件夹中找image.jpg,这时候就会出错。
4.RequestRequestrequest.setAttribute传递一些属性就需要用forward,如果想要跳转到别的应用的资源,就需要用sendRedirect。
5.如果不想刷新页面时,post重复提交数据,就是用服务器端跳转,forward方法
转载于CSDN
原文地址:https://www.cnblogs.com/wangjunxiao/p/8111185.html