JavaWeb学习心得之JSP属性范围

时间:2021-04-07 13:12:25

一、JSP属性范围

       JSP中的四种属性范围:

  • 当前页:属性只能在一个页面中取得,跳转到其他页面无法取得
  • 一次服务器请求:一个页面设置的属性,只要经过服务器跳转,则跳转之后的页面可以继续取得
  • 一次会话:一个用户设置的内容,只要与此用户相关的页面都可以访问。
  • 上下文:在整个服务器上设置的属性,所有人都可以访问
二、属性的操作方法
JavaWeb学习心得之JSP属性范围

三、JSP属性范围具体说明
1.page属性范围(pageContext)
说明:在一个页面设置的属性,跳转到其他页面就无法访问了
JavaWeb学习心得之JSP属性范围
示例1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//此时设置的属性只能够在本页中取得
pageContext.setAttribute("name","韩信");
pageContext.setAttribute("date",new Date());
%>
<%
//取得设置的属性
String name = (String)pageContext.getAttribute("name");
Date date = (Date)pageContext.getAttribute("date");
%>
<p><%=name %></p>
<p><%=date %></p>
</body>
</html>
访问结果:
JavaWeb学习心得之JSP属性范围
示例2
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
pageContext.setAttribute("name","韩信");
pageContext.setAttribute("date", new Date());
%>
<jsp:forward page="/pageContextDemo03.jsp"></jsp:forward>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8" 	import="java.util.Date"%><!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">    <title>test</title></head><body>	<%		String name = (String)pageContext.getAttribute("name");		Date date = (Date)pageContext.getAttribute("date");	%>	<p><%=name%></p>	<p><%=date%></p></body></html>
JavaWeb学习心得之JSP属性范围

2.request属性范围
说明:表示在一次服务器跳转有效,只要是服务器跳转,则设置request属性可以一直传递下去。
JavaWeb学习心得之JSP属性范围
步骤一:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//此时设置的属性只能够在本页中取得
request.setAttribute("name","韩信");
request.setAttribute("date",new Date());
%>
<jsp:forward page="/requestScopeDemo02.jsp"></jsp:forward>
</body>
</html>

步骤二:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//获取属性
String name = (String)request.getAttribute("name");
Date date = (Date)request.getAttribute("date");
%>
<p><%=name %></p>
<p><%=date %></p>
</body>
</html>
JavaWeb学习心得之JSP属性范围
步骤3:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//获取属性
String name = (String)request.getAttribute("name");
Date date = (Date)request.getAttribute("date");
%>
<p>第二次跳转</p>
<p><%=name %></p>
<p><%=date %></p>
</body>
</html>
JavaWeb学习心得之JSP属性范围

步骤4:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//获取属性
String name = (String)request.getAttribute("name");
Date date = (Date)request.getAttribute("date");
%>
<p>超链接跳转</p>
<p><%=name %></p>
<p><%=date %></p>
</body>
</html>
JavaWeb学习心得之JSP属性范围


3.session属性范围
说明:不管如何跳转都可以获取。
JavaWeb学习心得之JSP属性范围
步骤一:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//设置属性
session.setAttribute("name","韩信");
session.setAttribute("date", new Date());
%>
<jsp:forward page="/sessionScopeDemo02.jsp"></jsp:forward>
</body>
</html>
步骤2:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//设置属性
String name = (String)session.getAttribute("name");
Date date = (Date)session.getAttribute("date");
%>
<p>服务器跳转</p>
<p><%=name %></p>
<p><%=date %></p>
</body>
</html>
JavaWeb学习心得之JSP属性范围
步骤3:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//设置属性
String name = (String)session.getAttribute("name");
Date date = (Date)session.getAttribute("date");
%>
<p>超链接跳转</p>
<p><%=name %></p>
<p><%=date %></p>
</body>
</html>
JavaWeb学习心得之JSP属性范围
4.application属性范围
说明:任何人都可以获取属性
JavaWeb学习心得之JSP属性范围

步骤1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//设置属性
application.setAttribute("name","韩信");
application.setAttribute("date", new Date());
%>
<a href="${pageContext.request.contextPath}/applicationScopeDemo02.jsp">跳转</a>
</body>
</html>
步骤2:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//设置属性
String name = (String)application.getAttribute("name");
Date date = (Date)application.getAttribute("date");
%>
<p><%=name %></p>
<p><%=date %></p>
</body>
</html>

JavaWeb学习心得之JSP属性范围

注意:
  • 开启多个浏览器窗口,访问applicationScopeDemo02.jsp时,都可以获取属性。
  • 重启Tomcat后,再访问applicationScopeDemo02.jsp时,无法获取属性。

四、补充
方法:public abstract void setAttribute(String name,Object value,int  scope)此方法中存在一个scope的整型变量,此变量就表示一个属性的保存范围。
JavaWeb学习心得之JSP属性范围
JavaWeb学习心得之JSP属性范围