• ServletConfig 和 ServletContext


    1 ServletConfig

    1.1 ServletConfig 介绍

    • ServletConfig 是 Servlet 的配置参数对象,在 Servlet 的规范中,允许为每一个 Servlet 都提供一些初始化的配置。所以,每个 Servlet 都有一个自己的 ServletConfig。
    • 作用:在 Servlet 的初始化时,把一些配置信息传递给 Servlet。
    • 生命周期:和 Servlet 相同

    1.2 ServletConfig 配置方式

    • 配置ServletConfig
      在这里插入图片描述

    1.3 ServletConfig 常用方法

    在这里插入图片描述

    package com.txt.servlet;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Enumeration;
    
    public class ServletConfigDemo extends HttpServlet {
    
        //声明ServletConfig对象
        private ServletConfig config;
    
        //通过 init 方法,来对 ServletConfig 对象赋值
        @Override
        public void init(ServletConfig config) throws ServletException {
            this.config = config;
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //演示 ServletConfig 常用方法
            //根据 key 获取对应的 value 值
            String encodingValue = config.getInitParameter("encoding");
            System.out.println(encodingValue);
    
            //获取所有的key
            Enumeration<String> keys = config.getInitParameterNames();
            while (keys.hasMoreElements()){
                //获取每一个key
                String key = keys.nextElement();
                //根据key获取value
                String value = config.getInitParameter(key);
    
                System.out.println(key + "," + value);
            }
    
            //获取 Servlet 的名称
            String servletName = config.getServletName();
            System.out.println(servletName);
    
            //获取 ServletContext 对象
            ServletContext servletContext = config.getServletContext();
            System.out.println(servletContext);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    • 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
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    2 ServletContext

    2.1 ServletContext 介绍

    • ServletContext 是应用上下文对象(应用域对象)。每一个应用中只有一个 ServletContext 对象
    • 作用:可以配置和获得应用的全局初始化参数,可以实现 Servlet 之间的数据共享
    • 生命周期:应用加载则创建,应用停止则销毁

    2.2 域对象

    • 域对象指的是对象有作用域。也就是有作用范围。域对象可以实现数据的共享。不同作用范围的域对象,共享数据的能力也不一样
    • 在 Servlet 规范中,一共有 4 个域对象。ServletContext 就是其中一个。它也是 web 应用中最大的作用域,也叫 application 域。它可以实现整个应用之间的数据共享!

    2.3 ServletContext 配置方式

    • 标签中,通过 标签来配置。有两个子标签
    • :代表全局初始化参数的key
    • :代表全局初始化参数的value
      在这里插入图片描述

    2.4 ServletContext 常用方法

    • 获取方法
      在这里插入图片描述
      在这里插入图片描述
    package com.txt.servlet;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class ServletContextDemo extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //1. 获取 ServletContext 对象
            ServletContext context = getServletContext();
    
            //2. 常用方法的演示
            //获取全局配置参数:getInitParameter(String key) 根据 key 获取 value
            String globalDesc = context.getInitParameter("globalDesc");
            System.out.println(globalDesc);
    
            //获取应用的虚拟目录:getContextPath()
            String contextPath = context.getContextPath();
            System.out.println(contextPath);
    
            //根据虚拟目录获取绝对路径:getRealPath(String path)
            String realPath = context.getRealPath("/");
            System.out.println(realPath);
    
            String b = context.getRealPath("/b.txt");
            System.out.println(b);
    
            String c = context.getRealPath("/WEB-INF/c.txt");
            System.out.println(c);
    
            String a = context.getRealPath("/WEB-INF/classes/a.txt");
            System.out.println(a);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    • 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
    • 42
    • 43
    • 44
    • 45
    • 数据共享
      在这里插入图片描述
    		//设置共享数据
            context.setAttribute("username","zhangsan");
    
            //删除共享数据
            context.removeAttribute("username");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    		//获取共享数据
    		Object username = servletContext.getAttribute("username");
            System.out.println(username);
    
    • 1
    • 2
    • 3
  • 相关阅读:
    目标检测YOLO实战应用案例100讲-基于改进YOLOv6的轧钢表面细小缺陷检测
    [MRCTF2020]你传你呢
    图论算法<三>:判断有向图中是否有存在循环 ,以及环的个数和各个环中的元素
    11.3面试相关
    让 Visual Studio 用上 ChatGPT
    java国密加密SM2
    初识Spring(一)IOC
    【腾讯云原生降本增效大讲堂】Kubernetes资源拓扑感知调度
    快鲸scrm助力企业更快速实现成交转化
    基于PHP+MySQL的图书分享平台
  • 原文地址:https://blog.csdn.net/m0_59620032/article/details/128080087