四种属性范围

属性范围概述

Page只在一个页面中保存属性,跳转之后无效。

Request只在一次请求中保存属性,服务器跳转后依然有效。

Session在一次会话范围中保存,无论何种跳转都有效。但是在新开浏览器无法使用。

Applictation在整个服务器上保存,所有用户都可以使用。

4个内置对象都支持以下三种操作方法:

NO

方法名称

类型

描述

1

Public  void setAttribute(String name, Object o)

普通

设置属性的名称及内容

2

Public  Object getAttribute(String name)

普通

取得属性内容

3

Public  void removeAttribute(String name)

普通

删除属性

 

1.page属性范围(pageContext)

在当前页面设置属性有效,进行页面跳转则无效

例1 在页面内设置属性并取得属性

<%--page属性:设置当前页面才有效,进行跳转则无效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>page_scope</title>
</head>
<body>
<%
	//设置page属性范围,此属性只在JSP当前页面有效
	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>


例2 在页面内设置属性但跳转页面取得属性(不成功)

<%--page属性:设置当前页面才有效,进行跳转则无效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>page_scope</title>
</head>
<body>
<%
	//设置page属性范围,此属性只在JSP当前页面有效
	pageContext.setAttribute("name", "郑廉晨");
	pageContext.setAttribute("birthday", new Date());
%>

<jsp:forward page="page_scope_03.jsp" />		<%--进行服务器跳转--%>

</body>
</html>
<%--page属性:设置当前页面才有效,进行跳转则无效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>page_scope</title>
</head>
<body>

<%
	//从当前页面取出属性,并执行向下转型操作
	String username = (String) pageContext.getAttribute("name");
	Date userbirthday = (Date) pageContext.getAttribute("birthday");
%>

<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>

</body>
</html>


2.request属性

服务器跳转属性有效,但客户端跳转属性无效。

例1 服务器跳转属性有效

<%--request属性: 服务器跳转属性有效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>request_scope</title>
</head>
<body>
<%
	//设置request属性范围,此属性服务器跳转属性有效
	request.setAttribute("name", "郑廉晨");
	request.setAttribute("birthday", new Date());
%>

<jsp:forward page="request_scope_02.jsp" />		<%--进行服务器跳转--%>

</body>
</html>
<%--request属性: 服务器跳转属性有效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>request_scope</title>
</head>
<body>

<%
	//从当前页面取出属性,并执行向下转型操作
	String username = (String) request.getAttribute("name");
	Date userbirthday = (Date) request.getAttribute("birthday");
%>

<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>

</body>
</html>

例2 客户端跳转属性无效

<%
	//设置request属性范围,此属性服务器跳转属性有效
	request.setAttribute("name", "郑廉晨");
	request.setAttribute("birthday", new Date());
%>

<!--通过超链接跳转,属于客户端跳转,地址栏有变化哦-->
<a href="request_scope_02.jsp">通过超链接取得属性</a>

</body>
</html>

<!--通过超链接跳转,属于客户端跳转,地址栏有变化哦-->
<a href="request_scope_02.jsp">通过超链接取得属性</a>

</body>
</html>
<%--request属性: 服务器跳转属性有效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>request_scope</title>
</head>
<body>

<%
	//从当前页面取出属性,并执行向下转型操作
	String username = (String) request.getAttribute("name");
	Date userbirthday = (Date) request.getAttribute("birthday");
%>

<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>

</body>
</html>

3.session属性

session属性在一个浏览器中属性都有效,无论是服务器跳转还是客户端跳转都有效。

<%--session属性: 在一个浏览器中属性都有效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>request_scope</title>
</head>
<body>
<%
	//设置request属性范围,此属性服务器跳转属性有效
	session.setAttribute("name", "郑廉晨");
	session.setAttribute("birthday", new Date());
%>

<!--通过超链接跳转,属于客户端跳转,地址栏有变化哦-->
<a href="session_scope_02.jsp">通过超链接取得属性</a>

</body>
</html>
<%--session属性: 在一个浏览器中属性都有效--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>request_scope</title>
</head>
<body>

<%
	//从当前页面取出属性,并执行向下转型操作
	String username = (String) session.getAttribute("name");
	Date userbirthday = (Date) session.getAttribute("birthday");
%>

<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>

</body>
</html>

但是新开浏览器则属性无法获得(要关了原来开的浏览器哦)

4.application属性

application属性范围,此属性保存在服务器上

<%--application属性范围,此属性保存在服务器上--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>application_scope</title>
</head>
<body>

<%
	//从当前页面取出属性,并执行向下转型操作
	String username = (String) application.getAttribute("name");
	Date userbirthday = (Date) application.getAttribute("birthday");
%>

<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>

</body>
</html>
<%--application属性范围,此属性保存在服务器上--%>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>    <%--导java.util包--%>
<html>
<head>
	<title>application_scope</title>
</head>
<body>
<%
	//设置request属性范围,此属性服务器跳转属性有效
	application.setAttribute("name", "郑廉晨");
	application.setAttribute("birthday", new Date());
%>

<!--通过超链接跳转,属于客户端跳转,地址栏有变化哦-->
<a href="application_scope_02.jsp">通过超链接取得属性</a>

</body>
</html>

只要服务器没有重启则属性都有效,若服务器重启则无效。

Applictaion属性过多会影响服务器性能,慎用之。

所以使用属性原则

能用page则用page; 能request则用request;能session则用session;慎用application。


以上内容参考JAVAWEB开发实战经典(名师讲坛)