ServletContext对象的使用

一个web程序有一个对应的servletcontext对象,它代表当前的web应用
作用:

1.共享数据(一般不用,一般用session)

在一个servlet中的数据,可以通过servletcontext,在另一个servlet中获取。

public class SetServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext context = this.getServletContext();

        String username = "张三";

        context.setAttribute("context-name",username);
    }
}
public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");

        ServletContext context = this.getServletContext();

        String username = (String) context.getAttribute("context-name");

        resp.getWriter().print("名字"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

2.获取初始化参数

public class GetInitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext context =this.getServletContext();
        String url = context.getInitParameter("context-param-url");
        resp.getWriter().print(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  <!--配置初始化参数-->
  <context-param>
    <param-name>context-param-url</param-name>
    <param-value>www.baidu.com</param-value>
  </context-param>
  <servlet>
    <servlet-name>geti</servlet-name>
    <servlet-class>servlet.GetInitServlet</servlet-class>
  </servlet>
  <!--servlet的映射,配置访问路径-->
  <servlet-mapping>
    <servlet-name>geti</servlet-name>
    <url-pattern>/geti</url-pattern>
  </servlet-mapping>

结果:
在这里插入图片描述

3.请求转发

public class RequestDispathServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext context = this.getServletContext();

        /*
        //转发的请求路径
         RequestDispatcher requestDispatcher =  context.getRequestDispatcher("/hello");
        //调用forward实现请求转发
        requestDispatcher.forward(req,resp);
         */
        context.getRequestDispatcher("/hello").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  <servlet>
    <servlet-name>req</servlet-name>
    <servlet-class>servlet.RequestDispathServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>req</servlet-name>
    <url-pattern>/req</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("你好,servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

结果:
在这里插入图片描述
:请求转发不改变路径,重定向改变路径

读取资源文件(修改配置文件路径)

在java目录下的servlet包下新建一个abc.properties配置文件

username=root
password=123

在这里插入图片描述
由于abc.properties配置文件不是在约定位置resources目录下,所以需要在pom中进行配置,防止abc.properties配置文件无效。

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

新建servlet测试资源文件读取

public class GetPropertiesServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext context = this.getServletContext();
        //classes目录对应main下的java以及resources目录
        InputStream inputStream = context.getResourceAsStream("/WEB-INF/classes/servlet/abc.properties");

        Properties properties = new Properties();
        properties.load(inputStream);

        String user = properties.getProperty("username");
        String pwd = properties.getProperty("password");

        resp.getWriter().print(user+":"+pwd);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

配置web映射

  <servlet>
    <servlet-name>getp</servlet-name>
    <servlet-class>servlet.GetPropertiesServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>getp</servlet-name>
    <url-pattern>/getp</url-pattern>
  </servlet-mapping>

结果:
在这里插入图片描述

读取资源文件(默认配置文件路径)

abc.properties创建在约定的目录下,则无需在pom中进行修改配置
在这里插入图片描述
此时访问路径为:

InputStream inputStream = context.getResourceAsStream("/WEB-INF/classes/abc.properties");
posted @ 2022-01-20 23:06  ︶ㄣ演戲ㄣ  阅读(4)  评论(0编辑  收藏  举报  来源