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

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

作者: 小二上酒8 | 来源:发表于2022-09-21 13:16 被阅读0次

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

一、概念

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

二、pageContext作用域

1、仅限于单页面有效

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

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

(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>

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>

三、request请求作用域

1、仅在一次请求中保存

在服务器内部跳转有效

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

(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>

②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>

③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>

四、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>

②Microsoft Edge浏览器打开

五、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>

任意浏览器都可打开

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

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

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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/vedwortx.html