其他分享
首页 > 其他分享> > 6-ServletContext

6-ServletContext

作者:互联网

web容器启动时,它会为每一个工程创建一个ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象,它的作用很强大,代表整个web应用

下面例子的Servlet要设置mapping映射哈,可以自己设置

1、数据共享

测试:创建两个Servlet类,HelloServlet.class存属性,GetServlet.class取属性,(Servlet要设置mapping映射哈,这儿不展示了)

HelloServlet.class的doGet方法

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    //设置context属性
    context.setAttribute("username","yuan");
}

GetServlet.class的doGet方法

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    //获取属性
    String username = (String)context.getAttribute("username");
    //设置响应格式
    resp.setCharacterEncoding("utf-8");
    resp.setContentType("text/html");
    resp.getWriter().println("名字"+username);
}

2、获取参数

测试:在web.xml中配置一些初始化参数,利用ServletContext取出
web.xml

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

doGet方法

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    //获得指定配置。配置在web.xml中
    String parameter = context.getInitParameter("url");
    resp.getWriter().println(parameter);
}

3、请求转发

当我们访问这个Servlet时,它会进行转发,让我们访问其他路径(比如其他Servlet虚拟路径)。但此时我们显示的网址路径不会变(跟重定向不同)

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    //指定转发路径
    RequestDispatcher dispatcher = context.getRequestDispatcher("/demo3");
    //将请求和响应转发
    dispatcher.forward(req,resp);
}

4、读取properties文件

我们利用ServletContext将配置文件转为流,之后进行加载,就可以获得Properties对象了。

但首先,我们要找到打包后的网络资源中的这个配置文件。
对于运行时的网络资源,我们应该去生成的打包好的web目录下去找,即target中的servlet02-1.0-SNAPSHOT。

在这里插入图片描述

1、当我们把配置文件放在resources目录下时(resources root)
我们发现在打包的项目servlet02-1.0-SNAPSHOT下的WEB-INF下的classes下找到了我们的配置文件。
2、当我们把配置文件放在java目录下时(sources root)
我们刚开始并没有在项目下找到生成的配置文件,这是因为maven的约定大于配置,把配置文件给过滤掉了,因此我们要在pom中配置,允许properties文件生成

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    //获取资源并转为流
    InputStream stream = context.getResourceAsStream("/WEB-INF/classes/db.properties");
    //获取配置
    Properties properties = new Properties();
    properties.load(stream);
    resp.getWriter().println(properties.getProperty("username"));
    resp.getWriter().println(properties.getProperty("pwd"));
}

标签:ServletContext,配置文件,resp,context,doGet,properties
来源: https://blog.csdn.net/qq_39037116/article/details/120472971