• 服务器三大作用域:Request,ServletContext,Session


    context: 作用域

    作用域是在服务器运行阶段时在内存中开辟的空间, 用于存储当前的信息(数据)!

    Request 请求作用域

    • 生命周期 短暂

    创建: 当客户端发出请求时服务器会为当前的请求开辟一块存储空间!

    销毁: 当服务器返回响应时, 立即从服务器端销毁请求作用域!

    • 常用方法
    package com.javakc.web1.context;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet(
            name = "UserServlet",
            urlPatterns = "/user.do",
            loadOnStartup = 0
    )
    public class UserServlet extends HttpServlet {
    
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //从作用域中获取属性值
            Object username1 = request.getAttribute("username");
            System.out.println("第一次从请求作用于获取属性:" + username1);
    
            //向作用域中写入属性值
            request.setAttribute("username", "admin");
    
            //从作用域中获取属性值
            Object username2 = request.getAttribute("username");
            System.out.println("第二次从请求作用于获取属性:" + username2);
    
            //从作用域中移除属性
            request.removeAttribute("username");
    
            //从作用域中获取属性值
            Object username3 = request.getAttribute("username");
            System.out.println("第三次从请求作用于获取属性:" + username3);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    运行结果:
    第一次从请求作用于获取属性:null
    第二次从请求作用于获取属性:admin
    第三次从请求作用于获取属性:null
    
    • 1
    • 2
    • 3
    • 4
    • 使用场景
      涉及到数据量较为庞大
      临时性数据

    ServletContext 应用作用域

    在这里插入图片描述

    • 生命周期

    创建: 在服务器启动阶段时, 创建一个全新的应用作用域空间, 用于该应用中数据共享!

    销毁: 在服务器关闭阶段时, 销毁应用作用域!

    • 常用方法
    package com.javakc.web1.context;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet(
            name = "UserServlet",
            urlPatterns = "/user.do",
            loadOnStartup = 0
    )
    public class UserServlet2 extends HttpServlet {
    
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取应用作用域
            ServletContext context = request.getServletContext();
    
            //从应用作用域获取属性
            Object username1 = context.getAttribute("username");
            System.out.println("第一次从请求作用于获取属性:" + username1);
    
            //向应用作用域中写入属性
            context.setAttribute("username", "admin");
    
            //从应用作用域获取属性
            Object username2 = context.getAttribute("username");
            System.out.println("第二次从请求作用于获取属性:" + username2);
    
            //从应用作用域中移除属性
            context.removeAttribute("username");
    
            //从作用域中获取属性值
            Object username3 = context.getAttribute("username");
            System.out.println("第三次从请求作用于获取属性:" + username3);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    运行结果:
    第一次从请求作用于获取属性:null
    第二次从请求作用于获取属性:admin
    第三次从请求作用于获取属性:null
    
    • 1
    • 2
    • 3
    • 4
    • 应用场景
      适合共享的数据
      不适合临时性数据,隐私性数据

    Session 会话作用域

    • 生命周期
      ​创建: 在用户需要存储查询隐私性的内容时候, 动态创建会话作用域!
    package com.javakc.web1.context;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @WebServlet(
            name = "UserServlet",
            urlPatterns = "/user.do",
            loadOnStartup = 0
    )
    //会话作用域创建
    public class UserServlet3 extends HttpServlet {
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.假如客户端没有携带凭证(cookie), 服务器会创建全新的会话作用域
            //2.假如客户端携带了凭证(cookie), 服务器会获取之前创建过会话作用域
            HttpSession session1 = request.getSession();
            HttpSession session2 = request.getSession(true);
    
            //1.假如客户端没有携带凭证(cookie), 服务器会返回null
            //2.假如客户端携带了凭证(cookie), 服务器会获取之前创建过会话作用域
            HttpSession session3 = request.getSession(false);
    
            //判断会话作用域是否是新创建的
            HttpSession session4 = request.getSession();
            System.out.println(session4.isNew());
            System.out.println(session4.getId());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    销毁

    • 全局会话销毁方式 保证服务器不会出现内存溢出

    web.xml

      <session-config>
            
            <session-timeout>30session-timeout>
        session-config>
    
    • 1
    • 2
    • 3
    • 4
    • 单独销毁 立即销毁
    package com.javakc.web1.context;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @WebServlet(
            name = "LogoutServlet",
            urlPatterns = "/logout.do",
            loadOnStartup = 0
    )
    //会话作用域销毁
    public class UserServlet4 extends HttpServlet {
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpSession session = request.getSession(false);
    
            //单独销毁,立即销毁
            if (null != session) {
                //立即销毁
                session.invalidate();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 单独销毁 计时销毁
    package com.javakc.web1.context;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @WebServlet(
            name = "LogoutServlet",
            urlPatterns = "/logout.do",
            loadOnStartup = 0
    )
    //会话作用域销毁
    public class UserServlet4 extends HttpServlet {
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpSession session = request.getSession(false);
           
            //单独销毁,计时销毁
            if (null != session) {
                //倒计时十分钟后销毁
                session.setMaxInactiveInterval(60 * 10);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    Linux——Shell脚本编程(2)
    【Datahub】一站式元数据管理平台:Datahub宝典
    1、通过MeshDescription在运行时构建StaticMesh并设置碰撞、修改材质
    Java面试题:多线程面试必刷题,线程池是如何实现线程复用的?
    WordPress Mixed Content混合内容警告:4 种简单的修复方法
    springboot整合nacos
    通讯录管理系统-C++课程设计
    四、 【源码】数据源的解析、创建和使用
    GEE开发之Landsat8计算NDWI和数据分析
    文件读取结束的判定
  • 原文地址:https://blog.csdn.net/qq_45939736/article/details/126859777