ServletContext January 27,2020

Posted 伍六柒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
}

 

以上是关于ServletContext January 27,2020的主要内容,如果未能解决你的问题,请参考以下文章

Response January 27,2020

Session January 27,2020

Request January 27,2020

HTTP协议 January 27,2020

Cookie January 27,2020

ES & JSTL January 27,2020