zoukankan      html  css  js  c++  java
  • ServletContext January 27,2020


    ## ServletContext对象:


    1. 概念:代表整个web应用,可以和程序的容器(服务器)来通信
    2. 获取:
      1. 通过request对象获取
        request.getServletContext();

      2. 通过HttpServlet获取
        this.getServletContext();

    /**
     * ServletContext 对象的获取
     */
    @WebServlet("/servletContextDemo1")
    public class ServletContextDemo1 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.通过request获取
            ServletContext servletContext1 = request.getServletContext();
            //2.通过HTTPServlet获取
            ServletContext servletContext2 = this.getServletContext();
    
            System.out.println(servletContext1);
            System.out.println(servletContext2);
            System.out.println(servletContext1 == servletContext2);//true
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
        }
    }

    3. 功能:
       1. 获取MIME类型:
          * MIME类型:在互联网通信过程中定义的一种文件数据类型
          * 格式: 大类型/小类型 text/html image/jpeg

          * 获取:String getMimeType(String file)

    /**
     * MIME 类型的获取
     */
    @WebServlet("/servletContextDemo2")
    public class ServletContextDemo2 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            //定义文件名称
            String name = "abc.jpg";
            //获取类型
            String mimeType = context.getMimeType(name);
            System.out.println(mimeType); //  image/jpeg
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
        }
    }

       2. 域对象:共享数据
          1. setAttribute(String name,Object value)
          2. getAttribute(String name)
          3. removeAttribute(String name)

            * ServletContext对象范围:所有用户所有请求的数据

    /**
     * ServletContext共享数据
     */
    @WebServlet("/servletContextDemo3")
    public class ServletContextDemo3 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            context.setAttribute("msg","123");
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
        }
    }
    @WebServlet("/servletContextDemo4")
    public class ServletContextDemo4 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            Object msg = context.getAttribute("msg");
            System.out.println(msg);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
        }
    }

       3. 获取文件的真实(服务器)路径
          1. 方法:String getRealPath(String path)

    /**
     *获取文件真实路径
     */
        @WebServlet("/servletContextDemo5")
    public class ServletContextDemo5 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String realPath1 = context.getRealPath("/c.txt");//src目录下的资源访问
            System.out.println(realPath1);
            String realPath2 = context.getRealPath("/WEB-INF/b.txt");//WEB-INF目录下的资源访问
            System.out.println(realPath2);
            String realPath3 = context.getRealPath("/WEB-INF/classes/a.txt");//web目录下的资源访问
            System.out.println(realPath3);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
        }
    }

  • 相关阅读:
    VSCode编辑器在开发时常用的插件
    表单的数据校验规则及使用记录
    Vuex的插件保持状态持久化
    VueCli3项目中模拟数据及配置代理转发
    CSS 模块化
    Vue中静态地址的使用方式
    Vue中自动化引入样式及组件样式穿透
    Vue中的全局混入或局部混入
    让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
    刚学玩原生JS,自己写了一个小游戏,希望在以后能不断地进步加以改善
  • 原文地址:https://www.cnblogs.com/yyanghang/p/12236048.html
Copyright © 2011-2022 走看看