(十二):request 属性范围 (JSP学习第三天)
request 属性范围
只在一次请求中保存,服务器跳转后仍然有效
设置request属性范围 request_scope_01.jsp
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.util.*" %> <html> <head> <title>测试</title> </head> <body> <% request.setAttribute("name","小明"); request.setAttribute("birthday",new Date()); %> <jsp:forward page="request_scope_02.jsp"/>//如果换成超链接就不能取得request属性 </body> </html>
跳转后的页面 :request_scope_02.jsp
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.util.*" %> <html> <head> <title>测试</title> </head> <body> <% String name=(String)request.getAttribute("name"); Date birthday=(Date)request.getAttribute("birthday"); %> <h1><%=name%></h1> <h2><%=birthday%></h2> </body> </html>