forward、sendRedirect及Jsp和Servlet之间的跳转 JSPServletOffice浏览器Microsoft
程序员文章站
2022-05-21 13:51:18
...
<jsp:forward>
使用同一个
request
,是在服务器端跳转,浏览器显示的是第一个页面的名字,因为客户端(浏览器)不知道它在服务器端跳转了。
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
Response.sendRedirect
是两个不同的
request
,是在客户端跳转,浏览器显示的是跳转后的页面的名字。因为它是从客户端跳转到第二个页面的。
使用
forward
时,由于只是发送一次
request
请求,
request
设置的属性(
setAttribute
)依然能保留在下一个页面。
使用
sendRedirect
时,由于发送两次
request
请求,所以在下一个不能获取
request
属性。但可以通过重写
URL
的方式将内容传递过去。
下面举例说明:
tian.jsp
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
%>
<jsp:forward page="fengshan.jsp" />
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
%>
<jsp:forward page="fengshan.jsp" />
donglan.jsp
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
%>
<a href="fengshan.jsp">fengshan</a>
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
%>
<a href="fengshan.jsp">fengshan</a>
bama.jsp
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
response.sendRedirect("fengshan.jsp");
%>
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
response.sendRedirect("fengshan.jsp");
%>
fengshan.jsp
<%=request.getAttribute("river")%><br/>
<%=request.getAttribute("country")%>
<%=request.getAttribute("river")%><br/>
<%=request.getAttribute("country")%>
redwater
daguaiwan
daguaiwan
null
null
null
null
null
null
结果分析:
tiane.jsp是通过forward跳转到fengshan.jsp的,这两个页面享用的是同一个request,故fengshan.jsp可以接收到tiane.jsp通过 request设置的属性。
而donglan.jsp是通过href发送了另外的request请求,bama.jsp是通过sendRedirect跳转,用的也是另外的request请求,故fengshan.jsp不可以接收到这两个页面通过 request设置的属性。
Jsp和Servlet之间的跳转:
从Jsp跳转Servlet可以使用<jsp:forward page=”URL” />或者response.sendRedirect(URL)都可以,它们之间的区别上面已经说明了。
从Servlet跳转Jsp:
² getRequestDispatcher的forward(request, response)方法,这里也只是发送了一次请求,可以在下一个页面接受request设置的属性。
² response.sendRedirect(URL),这里发送了两次请求,不可以在下一个页面接受request设置的属性。
例子如下:
servlettiane.jsp
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
%>
<jsp:forward page="/tiane" />
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
%>
<jsp:forward page="/tiane" />
servletbama.jsp
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
response.sendRedirect("/ForwardRedirect/tiane");
%>
<%request.setAttribute("river","redwater");
request.setAttribute("country","daguaiwan");
response.sendRedirect("/ForwardRedirect/tiane");
%>
Tiane.java
publicclass Tiane extends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.sendRedirect("/ForwardRedirect/fengshan.jsp");
this.getServletConfig().getServletContext()
.getRequestDispatcher("/fengshan.jsp").forward(request, response);
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
publicclass Tiane extends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.sendRedirect("/ForwardRedirect/fengshan.jsp");
this.getServletConfig().getServletContext()
.getRequestDispatcher("/fengshan.jsp").forward(request, response);
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
再在web.xml中配置servlet:
<servlet>
<servlet-name>tiane</servlet-name>
<servlet-class>com.dvnchina.test.Tiane</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tiane</servlet-name>
<url-pattern>/tiane</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>tiane</servlet-name>
<servlet-class>com.dvnchina.test.Tiane</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tiane</servlet-name>
<url-pattern>/tiane</url-pattern>
</servlet-mapping>
上一篇: libtiff编译x64版本
下一篇: 随感