0

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

 1 year ago
source link: https://blog.51cto.com/u_15773567/5703451
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

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

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

二、pageContext作用域

1、仅限于单页面有效

即使网址不变页面跳转了也不行

(1)一般来讲都习惯于将这种范围称为page范围,表示将一个属性设置在本页上,跳转之后无法取得

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

(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>
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_html_03
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>
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_java_04

三、request请求作用域

1、仅在一次请求中保存

在服务器内部跳转有效

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

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

(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>
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_java_06
②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>
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_html_07
③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>
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_java_08

四、session作用域

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

在一次会话范围中,无论何种跳转都可以使用[图片上传失败...(image-760381-1663812721726)]

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>
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_html_09
②Microsoft Edge浏览器打开
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_java_10
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_html_11

五、application作用域

1、开机就生效

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

(1)对不同的session(用户)、不同浏览器在一个服务器中始终有效[图片上传失败...(image-4c12a-1663812721726)]

(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>
任意浏览器都可打开
Java Web程序设计基础二(服务器交互篇——四大属性作用域)_web开发_12

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

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK