<jsp:forward>使用同一个request,是在服务器端跳转,浏览器显示的是第一个页面的名字,因为客户端(浏览器)不知道它在服务器端跳转了。
Response.sendRedirect是两个不同的request,是在客户端跳转,浏览器显示的是跳转后的页面的名字。因为它是从客户端跳转到第二个页面的。
forward:服务器端跳转
redirect:客户端跳转
使用forward时,由于只是发送一次request请求,request设置的属性(setAttribute)依然能保留在下一个页面。
使用sendRedirect时,由于发送两次request请求,所以在下一个不能获取request属性。但可以通过重写URL的方式将内容传递过去。
服务器端跳转forwarddemo.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.whpu.edu.cn</title></head>
<body>
<%
request.setAttribute("info","www.whpu.edu.cn");
request.setAttribute("name","whpu");
%>
<jsp:forward page = "test1.jsp"/>
</body>
</html>
客户端跳转hrefdemo.jsp
<%@ page contentType = "text/html" pageEncoding = "GBK" %>
<html>
<head><title>www.whpu.edu.cn</title></head>
<body>
<%
request.setAttribute("info","www.baidu.com");
request.setAttribute("name","www.google.ocm.hk");
%>
<a href = "test1.jsp">test1</a>
</body>
</html>
sendRedirectdemo.jsp
<%@ page contentType = "text/html" pageEncoding = "GBK" %>
<html>
<head><title>www.whpu.edu.cn</title></head>
<body>
<%
request.setAttribute("info","www.baidu.com");
request.setAttribute("name","www.google.ocm.hk");
response.sendRedirect("test1.jsp");
%>
</body>
</html>
测试代码test1.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.whpu.edu.cn</title></head>
<body>
<%="info内容:" + request.getAttribute("info")%><br>
<%="name内容:" + request.getAttribute("name")%>
</body>
</html>
测试结果:
info内容:www.whpu.edu.cn
name内容:whpu
name内容:whpu
info内容:null
name内容:null
name内容:null
info内容:null
name内容:null
forwarddemo.jsp是通过forward跳转到test1.jsp的,这两个页面享用的是同一个request,故test1.jsp可以接收到forwarddemo.jsp通过request设置的属性。
而hrefdemo.jsp是通过href发送了另外的request请求,hrefdemo.jsp是通过sendRedirect跳转,用的也是另外的request请求,故test1.jsp不可以接收到这两个页面通过 request设置的属性。
Jsp和Servlet之间的跳转:
从Jsp跳转Servlet可以使用<jsp:forward page=”URL” />或者response.sendRedirect(URL)都可以,它们之间的区别上面已经说明了。
从Servlet跳转Jsp:
1、getRequestDispatcher的forward(request, response)方法,这里也只是发送了一次请求,可以在下一个页面接受request设置的属性。
2、response.sendRedirect(URL),
这里发送了两次请求,不可以在下一个页面接受request设置的属性。
范例:
servletforwarddemo.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.whpu.edu.cn</title></head>
<body>
<%
request.setAttribute("info","www.whpu.edu.cn");
request.setAttribute("name","whpu");
%>
<jsp:forward page = "/test2"/>
</body>
</html>
servletsendRedirectdemo.jsp
<%@ page contentType = "text/html" pageEncoding = "GBK" %>
<html>
<head><title>www.whpu.edu.cn</title></head>
<body>
<%
request.setAttribute("info","www.baidu.com");
request.setAttribute("name","www.google.ocm.hk");
response.sendRedirect("/mldn/test2");
%>
</body>
</html>
Test2.java
package org;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Test2 extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.sendRedirect("/mldn/test1.jsp"); // 客户端跳转
//this.getServletConfig().getServletContext().getRequestDispatcher("/test1.jsp").forward(req ,resp); // 服务器端跳转
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req,resp);
}
}
web.xml
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.Test2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/test2</url-pattern>
</servlet-mapping>
测试结果:
客户端跳转
info内容:null
name内容:null
name内容:null
服务器端跳转
info内容:www.whpu.edu.cn
name内容:whpu
name内容:whpu