7) pageContext 隐式对象

2025 年 3 月 17 日 | 阅读 1 分钟
在 JSP 中,pageContext 是 PageContext 类型的隐式对象。pageContext 对象可用于从以下范围之一设置、获取或删除属性
  • 页面
  • 请求
  • session
  • 应用程序
在 JSP 中,page 范围是默认范围。

pageContext 隐式对象的示例

index.html

<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>

welcome.jsp

<html>
<body>
<% 

String name=request.getParameter("uname");
out.print("Welcome "+name);

pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);

<a href="second.jsp">second jsp page</a>

%>
</body>
</html>

second.jsp

<html>
<body>
<% 

String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);
out.print("Hello "+name);

%>
</body>
</html>

输出

jsp pageContext implicit object output 1 jsp pageContext implicit object output 2 jsp pageContext implicit object output 3