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);
        }
    }

  • 相关阅读:
    android videoView 加载等待
    LocalBroadcastManager
    sessionStorage 、localStorage
    javascript 数组、json连接
    properties 文件注意事项
    nutz 使用beetl
    [Git/Github] ubuntu 14.0 下github 配置
    【UNIX环境编程、操作系统】孤儿进程和僵尸进程
    【操作系统】进程间通信
    【操作系统】线程
  • 原文地址:https://www.cnblogs.com/yyanghang/p/12236048.html
Copyright © 2011-2022 走看看