jsp之pageContext属性范围

本文详细介绍了如何在Java Web开发中使用page范围来存储和访问数据,以及页面跳转后作用域失效的问题。通过示例演示了在不同jsp文件之间的数据传递限制,并解释了原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

page属性范围表示将一个属性设置在本页上,跳转之后无法取得。

示例:

page_scope_01.jsp


<%@pagecontentType="text/html; charset=utf-8"%>

<%@pageimport = "java.util.*"%>

<html>

<head><title>张龙翔Java高端培训</title></head>

<body>

<%

pageContext.setAttribute("name","李兴华");

pageContext.setAttribute("birthday",new Date());

%>

<%

String username = (String)pageContext.getAttribute("name");

Date userbirthday = (Date)pageContext.getAttribute("birthday");

%>

<h2>姓名:<%=username%></h2>

<h2>生日:<%=userbirthday%></h2>

</body>

</html> 


把上例改成:page_scope_02.jsp 转向 page_scope_03.jsp,会发现page范围在跳转后无效。

page_scope_02.jsp

<%@page contentType="text/html; charset=utf-8"%>

<%@page import = "java.util.*"%>

<html>

<head><title>张龙翔Java高端培训</title></head>

<body>

<%

pageContext.setAttribute("name", "李兴华");

pageContext.setAttribute("birthday", new Date());

%>

<jsp:forward page="page_scope_03.jsp"/>

</body>

</html> 


page_scope_03.jsp

<%@page contentType="text/html; charset=utf-8"%>

<%@page import = "java.util.*"%>

<html>

<head><title>张龙翔Java高端培训</title></head>

<body>

<%

String username = (String)pageContext.getAttribute("name");

Date userbirthday = (Date)pageContext.getAttribute("birthday");

%>

<h2>姓名: <%=username %></h2>

<h2>生日: <%=userbirthday %></h2>


</body>

</html> 


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值