test1.jsp
<%@page contentType="text/html" pageEncoding="utf-8"
%><!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>测试页</title>
</head>
<body>
<jsp:include page="test2.jsp"/>
<%=response.getHeader("refresh")%><br><br>
<%=request.getAttribute("info")%>
</body>
</html>
test2.jsp
<%@page contentType="text/html" pageEncoding="utf-8"
%>
<%
response.setHeader("refresh","10;URL=test3.jsp");
request.setAttribute("info","Hello World!!!");
%>
test3.jsp
<%@page contentType="text/html" pageEncoding="utf-8"
%><!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>www.baidu.com</title>
</head>
<body>
<h1>百度一下,你就知道!</h1>
</body>
</html>
浏览器访问test1.jsp,结果如下,为什么是null?
-------------------------------------------------------------------------------
test4.jsp
<%@page contentType="text/html" pageEncoding="utf-8"
%>
<%
response.setHeader("refresh","10;URL=test3.jsp");
%>
<jsp:forward page="test5.jsp"/>
test5.jsp
<%@page contentType="text/html" pageEncoding="utf-8"
%><!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>测试页</title>
</head>
<body>
<span style="font-size:30px">test5</span><br><br>
<%=response.getHeader("refresh")%>
</body>
</html>
浏览器访问test4.jsp,服务器端跳转到test5.jsp,response能正常获取header,且10秒后能跳转到test3.jsp
@李兴华
首先request.getAttribute("info")能取到值,这个不难理解,未发生客户端跳转都有效,而test1.jsp打印出来的response.getHeader("refresh")是空的,我们在test1.jsp和test2.jsp分别输出request和response的内存地址,发现response并不是同一个对象
而test5.jsp打印出来的response.getHeader("refresh")不是空的,同样打印出地址,发现response是同一个对象
而request内存值不一样,也能取的到,我猜应该request设置属性并不是简单的设置对象值,而是存在类似hashMap的数据结构中,具体的则要你分析tomcat实现HttpServerRequest接口的源代码了