forward、sendRedirect及Jsp和Servlet之间的跳转

<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
info内容: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设置的属性。

JspServlet之间的跳转:
Jsp跳转Servlet可以使用<jsp:forward page=”URL” />或者response.sendRedirect(URL)都可以,它们之间的区别上面已经说明了。
 
Servlet跳转Jsp
1、getRequestDispatcherforward(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
服务器端跳转
info内容:www.whpu.edu.cn
name内容:whpu


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值