关于ServletContext的理解

842 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

关于ServletContext的理解

1.基础概念

首先,我们需要明确的是,一个servlet对应一个ServletContext是错误的。事实上,一个web应用,只对应一个ServletContext,也就是说,ServletContext的作用域是全局的,它能够作用的范围是整个应用。明确这一个概念,是非常重要的。

2.作用

① 共享数据

明确一个web应用只有一个ServletContext后,我们就聊一聊ServletContext的作用是什么。在web应用部署成功后,servlet容器为应用创建了ServletContext实例,当服务器关闭时,其才被释放。在web中,一个用户可以有多个request;一个用户有一个session,而ServletContext,是所有的用户共用一个。所以,ServletContext的存在,就是为了存放必须的、重要的、所有用户需要共享的、线程又是安全的一些资源信息,这样不仅节省了空间,还提高了效率

image-20220404152713921.png

  1. 首先给大家看一下该项目的结构

    image-20220404160739583.png

  1. 在HelloServlet类中放置数据
 public class HelloServlet extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         ServletContext context = this.getServletContext();
         String username = "传奇";
         context.setAttribute("username",username);//将一个数据保存在ServletContext中,名为:username,值:username
     }
 }
  1. 在HetUsername中读取放置的数据

     public class GetServlet extends HttpServlet {
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
             ServletContext context = this.getServletContext();
             String  username = (String) context.getAttribute("username");
             
             resp.setContentType("text/html;charset:utf-8");
             
             
             resp.getWriter().print("名字:"+username);
         }
     }
    

    4.获取结果

    image-20220404161609093.png

    注意:此处省略了web.xml的配置,请自行配置

②获取初始化参数

1.web.xml中配置初始化参数

   <context-param>
         <param-name>url</param-name>
         <param-value>jdbc:mysql//localhost:3306/</param-value>
     </context-param>

2.在java类中拿到这些参数

 public class ServletDemo03 extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         ServletContext context = this.getServletContext();
         String url = context.getInitParameter("url");
         resp.getWriter().print(url);
     }
 }

③请求转发

当A无法直接访问到C,需要B作为中介时,ServletContext就可以发挥它的“中介作用”

image-20220404165304373.png 1.在java类中请求转发

 public class S extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         ServletContext context = this.getServletContext();
         context.getRequestDispatcher("/C").forward(req,resp);//请求转发的路径,调用forword实现请求转发
     }
 }

2.web.xml中的配置

     <servlet>
         <servlet-name>B</servlet-name>
         <servlet-class>com.liu.servlet.S</servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>B</servlet-name>
         <url-pattern>/B</url-pattern>
     </servlet-mapping>

④读取资源文件

Properties

  • 在java目录下新建P类
  • 在resources目录下新建properties

发现:都被打包到了同一个路径下:classes,我们称这个路径为classpath

思路:创建一个文件流,通过ServletContext().getResourceAsStream("src")

 username=root
 password=123465
 public class P extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); //此处替换为自己的.properties的路径
         Properties prop = new Properties();
         prop.load(is);
         String user = prop.getProperty("username");
         String pwd = prop.getProperty("password");
         resp.getWriter().print(user+":"+pwd);
     }
 }

注意:此处省略了web.xml的配置,请自行配置

作者:老6
qq:664474618
欢迎一起交流学习!