美文网首页
ServletContext概述

ServletContext概述

作者: 帅气十里不如你 | 来源:发表于2020-12-11 13:51 被阅读0次
  • 概念:代表整个web应用,可以和程序的容器(服务器)来通信
  • 获取:(获取ServletContext对象)
    • 通过request对象获取
      • request.getServletContext();
    • 通过HTTPServlet获取
      • this.getServletContext();
  • 代码实现
@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        //1.通过request.getServletContext获取
        ServletContext sc1 = request.getServletContext();
        //2.通过this.ServletContext获取
        ServletContext sc2 = request.getServletContext();

        System.out.println(sc1);
        System.out.println(sc2);

        System.out.println(sc1 == sc2);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        this.doPost(request,response);
    }
}

打印结果:
org.apache.catalina.core.ApplicationContextFacade@1dcdd11
org.apache.catalina.core.ApplicationContextFacade@1dcdd11
true

  • 功能:
    • 获取MIME类型:
      • MIME类型:在互联网通信过程中定义的一种文件数据类型
        • 格式: 大类型/小类型 text/html image/jpeg
      • 获取:String getMimeType(String file)
@WebServlet("/servletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //2.定义文件名
        String filename = "a.jpg";
        //3.获取MIME类型
        String mine = servletContext.getMimeType(filename);
        System.out.println(mine);//输出结果image/jpeg
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        this.doPost(request,response);
    }
}

  • 域对象:共享数据
    • setAttribute(String name,Object value)
    • getAttribute(String name)
    • removeAttribute(String name)
    • ServletContext对象范围:所有用户所有请求的数据

代码实现:

@WebServlet("/servletContextDemo3")
public class ServletContextDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("msg","sssmmmm");
    }

    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 {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //获取数据
        Object msg = servletContext.getAttribute("msg");//注意获取的是Object对象
        System.out.println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        this.doPost(request,response);
    }
}

  • 获取文件的真实路径(服务器)路径
    • 方法:String getRealPath(String path)
    • 代码实现
@WebServlet("/servletContextDemo5")
public class ServletContextDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //获取数据
        String aPath = servletContext.getRealPath("/a.txt");
        System.out.println(aPath);

        String bPath = servletContext.getRealPath("/WEB-INF/b.txt");
        System.out.println(bPath);

        String cPath = servletContext.getRealPath("/WEB-INF/classes/c.txt");
        System.out.println(cPath);

        /**
         * 三个输出的打印结果
         * E:\Computer\Tomcat\day15\out\artifacts\day15_war_exploded\a.txt
         * E:\Computer\Tomcat\day15\out\artifacts\day15_war_exploded\WEB-INF\b.txt
         * E:\Computer\Tomcat\day15\out\artifacts\day15_war_exploded\WEB-INF\classes\c.txt
         * 注意:打印的不是工作空间而是,打印的是真实路径,也就是服务器tomcat路径
         */
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        this.doPost(request,response);
    }
}

相关文章

网友评论

      本文标题:ServletContext概述

      本文链接:https://www.haomeiwen.com/subject/kwsjgktx.html