ServletContext对象、获取MIME类型、域对象、获取文件的真实路径

ServletContext对象:

1. 概念

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

2. 获取ServletContext对象

  1. 通过request对象获取
    request.getServletContext();
  2. 通过HttpServlet获取
    this.getServletContext();
@WebServlet("/contextServletDemo1")
public class ContextServletDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ServletContext对象获取:
        //1.通过request对象来获取
        ServletContext context1 = request.getServletContext();
        //2.通过HttpServlet获取
        ServletContext context2 = this.getServletContext();

        System.out.println(context1);
        System.out.println(context2);
        System.out.println(context1==context2);

        /*输出
        org.apache.catalina.core.ApplicationContextFacade@44e39f2e
        org.apache.catalina.core.ApplicationContextFacade@44e39f2e
        true
         */
    }

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

3.功能:

1.获取MIME类型:ServletContext功能:

  • MIME类型:
    在互联网通信过程中定义的一种文件数据类型

  • 格式:
    大类型/小类型
    text/html
    image/jpeg

获取: String getMimeType(String file)

@WebServlet("/contextServletDemo2")
public class ContextServletDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过HttpServlet获取
        ServletContext context = this.getServletContext();

        //定义文件名称
        String filename = "a.jpg";

        //获取MIME类型
        String mimeType = context.getMimeType(filename);
        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对象范围: 所有用户所有请求的数据

设置数据

@WebServlet("/contextServletDemo3")
public class ContextServletDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过HttpServlet获取
        ServletContext context = this.getServletContext();

        //设置数据
        context.setAttribute("msg","hello");
    }

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

获取数据

@WebServlet("/contextServletDemo4")
public class ContextServletDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过HttpServlet获取
        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.获取文件的真实(服务器)路径

@WebServlet("/contextServletDemo5")
public class ContextServletDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过HttpServlet获取
        ServletContext context = this.getServletContext();

        //获取文件的服务器路径
        String realPath = context.getRealPath("/a.txt");//web目录下资源访问
        System.out.println(realPath);
        //File file = new File(realPath);

        String pathb = context.getRealPath("/WEB-INF/b.txt");//WEB-INF目录下的
        System.out.println(pathb);

        String pathc = context.getRealPath("/WEB-INF/classes/c.txt");//src目录下的
        System.out.println(pathc);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值