zoukankan      html  css  js  c++  java
  • 4.ServletContext接口

    ServletContext接口简介:
    其实这个Context是上下文啊  我在学安卓的时候学过 ,这玩意很强大 是个全局的东西:

    WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,ServletContext对象包含Web应用中所有 Servlet 在 Web 容器中的一些数据信息ServletContext随着Web应用的启动而创建,随着 Web 应用的关闭而销毁一个 Web 应用只有一个ServletContext 对象
    ServletContext中不仅包含了 web.xml 文件中的配置信息,还包含了当前应用中所有Servlet可以共享的数据。可以这么说, ServeltContext 可以代表整个应用,所以ServletContext有另外一个名称:application

    ServletContext中常用方法

    ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext()方法获得ServletContext对象

    • String getInitParameter ():获取 web.xml 文 件 的 中 指 定 名 称 的上下文参数值 。
    • Enumeration getInitParameterNames():获取 web.xml 文件的中的所有的上下文参数名称。其返回值为枚举类型 Enumeration。
    • void setAttribute(String name, Object object):在 ServletContext 的公共数据空间中,也称为域属性空间,放入数据。这些数据对于 Web应用来说,是全局性的,与整个应用的生命周期相同。当然,放入其中的数据是有名称的,通过名称来访问该数据。
    • Object getAttribute(String name):从 ServletContext 的域属性空间中获取指定名称的数据。
    • void removeAttribute(String name):从 ServletContext 的域属性空间中删除指定名称的数据。
    • String getRealPath(String path):获取当前 Web 应用中指定文件或目录在本地文件系统中的路径。
    • String getContextPath():获取当前应用在 Web 容器中的名称。

    代码中详细讲解:

          1.web.xml代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>s1</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
      <servlet-name>s1</servlet-name>
      <servlet-class>s1.s1</servlet-class>
      
    
      
      </servlet>
      
      <servlet-mapping>
      <servlet-name>s1</servlet-name>
      <url-pattern>/s1</url-pattern>
      </servlet-mapping>
      
    
      <!-- ****************s1 和 s2 的 servlet 分界线**************************** -->
    
        <servlet>
      <servlet-name>s2</servlet-name>
      <servlet-class>s2.s2</servlet-class>
      
      <init-param>
      <param-name>s2_param_name</param-name>
      <param-value>s2_paramName_BiHu</param-value>
      </init-param>
      
      </servlet>
      
      <servlet-mapping>
      <servlet-name>s2</servlet-name>
      <url-pattern>/s2</url-pattern>
      </servlet-mapping>
      
      
      
      <!-- 非常注意这个啊! 这个是Context初始化参数值  他是context-param 标签 且  放在 全局下 不是放在Servlet中!  -->
        <context-param>
      <param-name>Context_param_name</param-name>
      <param-value>Context_paramName_BiHu</param-value>
      </context-param>
      
      
    </web-app>

          2.s1.java :

    package s1;
    
    import java.io.IOException;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class s1 implements Servlet {
        private ServletContext Context = null;
        
        @Override
        public void init(ServletConfig config) throws ServletException {
            Context = config.getServletContext();        //获取Servlet的上下文给Context
        }
    
        @Override
        public ServletConfig getServletConfig() {
    
            return null;
        }
    
        @Override
        public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
           /*
                         *  测试:1.获取 web.xml 文 件 的 中 指 定 名 称 的上下文参数值  (和初始化参数的原理一样的 但是位置不一样 标签页不一样而已)
            *  2.获取 web.xml 文件的中的所有的上下文参数名称。其返回值为枚举类型 Enumeration。 (这个和之前的一样 这里不列举了 自己试试!)
            * 3.通过名称来设置该 ServletContext 的公共空间(域属性空间)的数据 (和之前安卓的差不多啊 所以这个Context很强大)
            *  3.从 ServletContext 的域属性空间中获取指定名称的数据。
            *  3.从 ServletContext 的域属性空间中删除指定名称的数据。
            *  6.获取当前 Web 应用中指定文件或目录在本地文件系统中的路径。
            *  7.获取当前应用在 Web 容器中的名称。
            */
            
            
            //1.请注意在xml中的配置是放在全局 且 是<context-param>标签(不仅可以获取还可以设置 但这里不讲!)
            String ContextParamValue = Context.getInitParameter("Context_param_name");
            System.out.println("ContextParamValue : " + ContextParamValue);
                
            //2.这里不示范 主要还是和前面一样 用 
    //        Context.getInitParameterNames()这个方法  和 枚举类型的那两个方法即可! 
            
            //3.因为Context可以存储数据 而且他这个数据(键 - 值)是在整个项目(Servlet)都可以访问的到的 所以呢 这里设置数据和访问、删除。
            Context.setAttribute("数据1", "欢迎观看我的博客,我是BiHu! 谢谢!");    //设置
            String data1 = (String) Context.getAttribute("数据1");        //获取
            System.out.println(data1);
            Context.removeAttribute("数据1");            //删除数据
            
            data1 = (String) Context.getAttribute("数据1");        //删除后尝试再获取
            System.out.println(data1);                                        //失败! 返回null
            
            Context.setAttribute("数据1", "s2获取成功! 欢迎观看我的博客,我是BiHu! 谢谢!");    //设置给S2访问 证实一下是全局的        
            
            // 6.获取指定前 Web 应用中指定文件或目录在本地文件系统中的路径。 用
            System.out.println("此项目的绝对路径" + Context.getRealPath("s1"));        //填写上当前We应用名(项目名)即可打印出来他在硬盘的绝对路径 因为是在Eclipse 所以会在Eclipse的临时服务器存放的啊!
            
            //7.获取当前应用在 Web 容器中的名称。(就是那个网址8080后面加上的那个路劲 即: /项目名)
            System.out.println("此Web的路径" + Context.getContextPath());
        }
        
        @Override
        public String getServletInfo() {
            
            return null;
        }
    
        @Override
        public void destroy() {
            
    
        }
    
        public static void main(String[] args) {
            
    
        }
    
    }

          3. s2.java:

    package s2;
    
    import java.io.IOException;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    
    
    public class s2 implements Servlet {
        
        private ServletContext Context = null;
        
        @Override
        public void init(ServletConfig config) throws ServletException {
            
            Context = config.getServletContext();        //获取到上下文
        }
    
        @Override
        public ServletConfig getServletConfig() {
            
            return null;
        }
    
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            //那个Context 是全局的啊 所以先获取到 然后在这里获取它的属性即可
            System.out.println(Context.getAttribute("数据1"));    //名字别打错即可!
        }
    
        @Override
        public String getServletInfo() {
            
            return null;
        }
    
        @Override
        public void destroy() {
            
    
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
        }
    
    }

        注意看注释即可完成学习!!

    本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/14799981.html

  • 相关阅读:
    c语言实现双色球和大乐透
    字符串数组的三种内存模型
    c语言实现数组的排序
    C语言实现二级指针表示字符串数组
    c语言实现字符指针(字符串)数组的排序
    Windows Defender检查文件和应用要管理员设置
    java方法学习1
    The second day of studing English
    Selenium-通过classname定位注意的小问题
    Selenium-ChromeWebDriver
  • 原文地址:https://www.cnblogs.com/bi-hu/p/14799981.html
Copyright © 2011-2022 走看看