(十一):page属性的范围 (JSP学习第三天)
page属性范围一般使用pageContext表示,只在一个页面保存属性,跳转之后无效.
设置和取得page范围的属性:page_scope_01.jsp
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.util.*" %> <html> <head> <title>测试</titlte> </head> <body> <% //设置page属性 pageContext.setAttribute("name","小明"); pageContext.setAttribute("birthday",new Date()); %> <% //从page属性范围中取出属性 String username=(String)pageContext.getAttribute("name"); Date userbirthday=(Date)pageContext.getAttribute("birthday"); %> <h1><%=username%></h1> <h2><%=userbirthday%</h2> </body> </html>
如果使用跳转执行取得属性:page_scope_02.jsp
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.util.*" %> <html> <head> <title>测试</title> </head> <body> <% //设置page属性 pageContext.setAttribute("name","小明"); pageContext.setAttribute("birthday",new Date()); %> <jsp:forward page="page_scope_03.jsp"/> </body> </html>
跳转后的页面:page_scope_03.jsp
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.util.*" %> <html> <head> <title>测试</title> </head> <body> <% //从page属性范围中取出属性 String username=(String)pageContext.getAttribute("name"); Date userbirthday=(Date)pageContext.getAttribute("birthday"); %> <h1>姓名:<%=username%></h1> <h2>生日:<%=userbirthday%></h2> </body> </html>
则执行结果属性为null