码迷,mamicode.com
首页 > 其他好文 > 详细

ServletContext January 27,2020

时间:2020-01-27 15:38:57      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:info   style   ==   src   Servle   font   路径   tco   void   


## 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

标签:info   style   ==   src   Servle   font   路径   tco   void   

原文地址:https://www.cnblogs.com/yyanghang/p/12236048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!