ServletContext学习

12 篇文章 0 订阅

ServletContext对象

  1. 概念:代表整个Web应用,可以和程序的容器(服务器)来通信
  2. 获取:
    1. 通过request对象获取:request.getServletContext()
    2. 通过HttpServlet获取:this.getServletContext()
  3. 功能:
    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 {
        //1.通过HttpServlet获取
        ServletContext context = this.getServletContext();
        //2.定义文件名称
        String fileName = "a.jpg";
        //3.获取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. 域对象:共享数据
      • 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.通过HttpServlet获取
        ServletContext context = this.getServletContext();
        //2.共享数据
        context.setAttribute("msg","hello");
    }

    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.通过HttpServlet获取
        ServletContext context = this.getServletContext();
        //2.获取数据
        Object msg = context.getAttribute("msg");
        System.out.println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
  • 先访问Demo3再访问Demo4:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    1. 获取文件的真实(服务器)路径
      • 方法:String getRealPath(String path)
  • 资源位置:
    在这里插入图片描述

  • 代码:

@WebServlet("/servletContextDemo5")
public class ServletContextDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过HttpServlet获取
        ServletContext context = this.getServletContext();
        //2.获取文件的服务器路径
        String bPath = context.getRealPath("/a.txt");//web目录下资源访问
        System.out.println(bPath);
        String cPath = context.getRealPath("/WEB_INF/b.txt");//WEB_INF目录下的资源访问
        System.out.println(cPath);
        String aPath = context.getRealPath("/WEB_INF/classes/c.txt");//src目录下的资源访问
        System.out.println(aPath);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
  • 访问输出结果:
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值