ServletContext对象概述、ServletContext获取

ServletContext对象概述

  1,概念:代表整web应用,可以和程序容器(服务器)来通信

  2,功能:

    获取MIME类型

    域对象:共享数据

    获取文件的真实(服务器)路径  、

 

ServletContext获取

  获取:

    通过request对象来获取

      request.getServletContext();

    通过HttpServlet获取

      this.getServletContext()

   

案例:

  

@WebServlet(value = "/ServletContext")
public class ServletContext extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ServletContext对象的获取
/**
*   获取:
*
*     通过request对象来获取
*       request.getServletContext();
*     通过HttpServlet获取
*       this.getServletContext()
*/
// 通过request对象来获取
javax.servlet.ServletContext context = request.getServletContext();
// 通过HttpServlet获取
javax.servlet.ServletContext context1 = this.getServletContext();
System.out.println(context);
System.out.println(context1);
System.out.println(context==context1);

}
}

 

posted @ 2022-08-15 16:04  一位程序袁  阅读(81)  评论(0编辑  收藏  举报