说一下 jsp 的 4 种作用域?

本文详细介绍了JSP中的四种作用域(PageScope、RequestScope、SessionScope和ApplicationScope),展示了如何在这些作用域中存储和共享数据,以及选择合适作用域的依据。
摘要由CSDN通过智能技术生成

说一下 jsp 的 4 种作用域?

在 JSP(JavaServer Pages)中,有四种作用域,它们决定了对象的可见性和生命周期。这四种作用域分别是:

  1. 页面作用域(Page Scope):

    • 页面作用域表示对象的生命周期与当前 JSP 页面的请求处理周期相同。
    • 页面作用域中的对象只能在当前页面的多个地方访问。
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Page Scope Example</title>
    </head>
    <body>
        <%@ page import="java.util.ArrayList" %>
        <% 
            // 在页面作用域中创建一个 ArrayList 对象
            ArrayList<String> pageList = new ArrayList<>();
            pageList.add("Item 1");
            pageContext.setAttribute("pageList", pageList);
        %>
        <h1>Page Scope Example</h1>
        <p>Items in pageList:</p>
        <ul>
            <% 
                // 在页面作用域中获取并显示 ArrayList 对象
                ArrayList<String> retrievedList = (ArrayList<String>) pageContext.getAttribute("pageList");
                for (String item : retrievedList) {
                    out.println("<li>" + item + "</li>");
                }
            %>
        </ul>
    </body>
    </html>
    
  2. 请求作用域(Request Scope):

    • 请求作用域表示对象在同一个 HTTP 请求内是可见的。
    • 请求作用域中的对象可以在一个 JSP 页面和它所转发请求的下一个页面之间共享。
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Request Scope Example</title>
    </head>
    <body>
        <%@ page import="java.util.HashMap" %>
        <%
            // 在请求作用域中创建一个 HashMap 对象
            HashMap<String, String> requestMap = new HashMap<>();
            requestMap.put("key1", "Value 1");
            request.setAttribute("requestMap", requestMap);
        %>
        <h1>Request Scope Example</h1>
        <p>Value for key1: <%= request.getAttribute("requestMap").get("key1") %></p>
    </body>
    </html>
    
  3. 会话作用域(Session Scope):

    • 会话作用域表示对象在用户的整个会话期间是可见的,即用户打开浏览器到关闭浏览器。
    • 会话作用域中的对象可以在一个 Web 应用程序的不同页面之间共享。
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Session Scope Example</title>
    </head>
    <body>
        <%@ page import="java.util.HashSet" %>
        <%
            // 在会话作用域中创建一个 HashSet 对象
            HashSet<String> sessionSet = new HashSet<>();
            sessionSet.add("Item A");
            session.setAttribute("sessionSet", sessionSet);
        %>
        <h1>Session Scope Example</h1>
        <p>Items in sessionSet:</p>
        <ul>
            <%
                // 在会话作用域中获取并显示 HashSet 对象
                HashSet<String> retrievedSet = (HashSet<String>) session.getAttribute("sessionSet");
                for (String item : retrievedSet) {
                    out.println("<li>" + item + "</li>");
                }
            %>
        </ul>
    </body>
    </html>
    
  4. 应用程序作用域(Application Scope):

    • 应用程序作用域表示对象在整个 Web 应用程序的生命周期内是可见的,即从应用程序启动到关闭。
    • 应用程序作用域中的对象可以在一个 Web 应用程序的所有页面之间共享。
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Application Scope Example</title>
    </head>
    <body>
        <%@ page import="java.util.LinkedHashMap" %>
        <%
            // 在应用程序作用域中创建一个 LinkedHashMap 对象
            LinkedHashMap<String, String> appMap = new LinkedHashMap<>();
            appMap.put("keyX", "Value X");
            application.setAttribute("appMap", appMap);
        %>
        <h1>Application Scope Example</h1>
        <p>Value for keyX: <%= application.getAttribute("appMap").get("keyX") %></p>
    </body>
    </html>
    

这些示例演示了如何在不同的作用域中存储和获取数据,以及数据在不同页面之间的共享。作用域的选择应该基于数据的生命周期和可见性的需求。

  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习资源网

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值