Java Web程序设计基础二(服务器交互篇——四大属性作用域)

615 阅读2分钟

在介绍JSP各个对象的使用前,先带大家认识一下四大属性作用域吧🧐

一、概念

JSP中提供了四种属性的保存范围,所谓的属性保存范围,指的就是一个设置的对象,可以在多少个页面中保存使用

image.png

二、pageContext作用域

1、 仅限于单页面有效

即使网址不变页面跳转了也不行
(1)一般来讲都习惯于将这种范围称为page范围,表示将一个属性设置在本页上,跳转之后无法取得

image.png

(2)代码例子 
page_scope_01.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%	// 设置属性
			pageContext.setAttribute("name","是申小兮呀") ;
			pageContext.setAttribute("birthday",new Date()) ;
		%>
		中间其它内容
		<hr />
		<%
			String username = (String) pageContext.getAttribute("name") ;
			Date userbirthday = (Date)pageContext.getAttribute("birthday") ;
		%>
		<h2>姓名:<%=username%></h2>
		<h2>日期:<%=userbirthday%></h2>
	</body>
</html>

image.png

page_scope_02.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%	// 设置属性
			pageContext.setAttribute("name","是申小兮的PageScope-forward") ;
			pageContext.setAttribute("birthday",new Date()) ;
		%>
		<jsp:forward page="page_scope_03.jsp"/>
	</body>
</html>

page_scope_03.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%
			String username = (String) pageContext.getAttribute("name") ;
			Date userbirthday = (Date)pageContext.getAttribute("birthday") ;
		%>
		<h2>姓名:<%=username%></h2>
		<h2>日期:<%=userbirthday%></h2>
	</body>
</html>

image.png

三、request请求作用域

1、 仅在一次请求中保存

在服务器内部跳转有效
(1)在服务器内部跳转之后,设置的内容依然会被保留下来

image.png

(2)a标签超链接跳转了的数据是不生效的,因为网址变了
(3)代码例子
①request_scope_01.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			跳转前1111111
		</title>
	</head>
	<body>
		<%	// 设置属性
			request.setAttribute("name","您好,申小兮") ;
			request.setAttribute("today",new Date()) ;
		%>
		<jsp:forward page="request_scope_02.jsp"/>
	</body>
</html>

request_scope_02.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			跳转后22222222
		</title>
	</head>
	<body>
	page 2222222
	<p>
		<%
			String username = (String) request.getAttribute("name") ;
			Date usertoday = (Date)request.getAttribute("today") ;
		%>
		<h2>姓名:<%=username%></h2>
		<h2>日期:<%=usertoday%></h2>
	</body>
</html>

image.png

②request_scope_03.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%	// 设置属性
			request.setAttribute("name","申小兮") ;
			request.setAttribute("today",new Date()) ;
		%>
		<a href="request_scope_02.jsp">通过链接取得属性</a>
	</body>
</html>

image.png

③request_scope_04.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%	// 设置属性,注意这里是pageContext对象,理论上应该是page作用域
			pageContext.setAttribute("name","申小兮",PageContext.REQUEST_SCOPE) ;
			pageContext.setAttribute("today",new Date(),PageContext.REQUEST_SCOPE) ;
		%>
		<jsp:forward page="request_scope_02.jsp"/>
	</body>
</html>

image.png

四、session作用域

1、仅同一个浏览器内有效

在一次会话范围中,无论何种跳转都可以使用 image.png

session_scope_01.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%	// 设置属性
			session.setAttribute("name","申小兮") ;
			session.setAttribute("today",new Date()) ;
			
		%>
		<a href="session_scope_02.jsp">通过链接取得属性</a>
	</body>
</html>

session_scope_02.jsp文件

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%
			String username = (String) session.getAttribute("name") ;
			Date usertoday = (Date)session.getAttribute("today") ;
		%>
		<h2>姓名:<%=username%></h2>
		<h2>生日:<%=usertoday%></h2>
	</body>
</html>

image.png

②Microsoft Edge浏览器打开

image.png

image.png

五、application作用域

1、 开机就生效

在整个服务器上保存,所有用户都可以使用
(1)对不同的session(用户)、不同浏览器在一个服务器中始终有效 image.png

(2)代码例子

application_scope_01.jsp

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%	// 设置属性
			application.setAttribute("name","申小兮的ApplicationScope") ;
			application.setAttribute("today",new Date()) ;
		%>
		<a href="application_scope_02.jsp">通过链接取得属性</a>
	</body>
</html>

application_scope_02.jsp

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<html>
	<head>
		<title>
			http://www.fit.edu.cn/,Java Web开发
		</title>
	</head>
	<body>
		<%
			String username = (String) application.getAttribute("name") ;
			Date usertoday = (Date)application.getAttribute("today") ;
		%>
		<h2>姓名:<%=username%></h2>
		<h2>生日:<%=usertoday%></h2>
	</body>
</html>

image.png

任意浏览器都可打开

image.png

<1>JSP提供了由容器实现和管理的内置对象,在JSP页面中可以直接使用,不需要实例化,通过存取这些内置对象实现与JSP页面的Servlet环境的相互访问

<2>JSP一共提供了九个内置对象:out、request、response、sesion、application、pageContext、config、page和exception

注意: 上面的四大属性作用域都属于内置对象