ServletContext 对象
发布日期:2021-05-07 19:41:30 浏览次数:17 分类:精选文章

本文共 3008 字,大约阅读时间需要 10 分钟。

ServletContext 对象代表整个web应用,可以和程序的容器(服务器)来通信

文章目录

一、获取方法

(1)通过 request 对象获取:request.getServletContext();(2)通过 HttpServlet 获取:this.getServletContext(); -- 更常用
@WebServlet("/servletContextDemo1")public class ServletContextDemo1 extends HttpServlet {       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           /*            ServletContext对象获取:                1. 通过request对象获取			        request.getServletContext();                2. 通过HttpServlet获取                    this.getServletContext();         */                //1. 通过request对象获取        ServletContext context1 = request.getServletContext();        //2. 通过HttpServlet获取        ServletContext context2 = this.getServletContext();        System.out.println(context1);        System.out.println(context2);    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           this.doPost(request,response);    }}

在这里插入图片描述

二、功能

  1. 获取 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 {                   //通过HttpServlet获取        ServletContext context = this.getServletContext();        //3. 定义文件名称        String filename = "a.jpg";//image/jpeg        //4.获取MIME类型        String mimeType = context.getMimeType(filename);        System.out.println(mimeType);    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           this.doPost(request,response);    }}
  1. 域对象:共享数据
(1)setAttribute(String name,Object value)(2)getAttribute(String name)(3)removeAttribute(String name)* ServletContext对象范围:所有用户所有请求的数据

和 共享数据一样,但是比 request 作用范围更广,所以需要谨慎使用

  1. 获取文件的真实(服务器)路径
方法:String getRealPath(String path)  				 * String b = context.getRealPath("/b.txt");//web目录下资源访问		   * String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问		    * String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问
@WebServlet("/demo")public class ResponseServlet2 extends HttpServlet {       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           ServletContext context = this.getServletContext();        String path1 = context.getRealPath("/WEB-INF/classes/a.txt");        String path2 = context.getRealPath("/b.txt");        String path3 = context.getRealPath("/WEB-INF/c.txt");        System.out.println("a.txt --> " + path1);        System.out.println("b.txt --> " + path2);        System.out.println("c.txt --> " + path3);    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           this.doPost(request, response);    }}

在这里插入图片描述

在这里插入图片描述
这里显示的服务器项目保存的路径
在这里插入图片描述

上一篇:文件下载案例
下一篇:验证码的简单实现

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月19日 09时45分09秒