前言:

本文内容:ServletContext对象、ServletContext应用

推荐免费JavaWeb入门到实战视频:【狂神说Java】JavaWeb入门到实战_哔哩哔哩_bilibili

ServletContext对象

Web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用:

131

  • 共享数据

    多个Servlet之间可以数据共享

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("hello");
    // this.getInitParameter(); 初始化参数
    // this.getServletConfig(); Servlet配置
    // this.getServletContext(); Servlet上下文
    ServletContext context = this.getServletContext();
    String username = "小王";
    // 将数据保存在ServletContext
    context.setAttribute("username",username);
    }

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

    读取的Servlet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    // 获取HelloServlet中传的值
    String username=(String) context.getAttribute("username");
    resp.setContentType("text/html;charset=utf-8");
    resp.getWriter().print("姓名:"+username);
    }

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

    配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <!--    注册Servlet-->
    <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.jokerdig.servlet.HelloServlet</servlet-class>
    </servlet>
    <!-- Servlet映射-->
    <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <!-- get注册-->
    <servlet>
    <servlet-name>get</servlet-name>
    <servlet-class>com.jokerdig.servlet.GetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>get</servlet-name>
    <url-pattern>/get</url-pattern>
    </servlet-mapping>

    测试值结果,Servlet之间通过ServletContext可以传递数据

ServletContext应用

获取初始化参数

配置初始化参数

1
2
3
4
5
<!--  配置web初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>

获取初始化参数

1
2
3
4
5
6
7
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().print(url);
}

请求转发

请求转发地址不会改变

重定向地址会改变

130

1
2
3
4
5
6
7
8
9
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了ServletDemo02");
resp.setContentType("text/html;charset=utf-8");
// 转发的请求路径
RequestDispatcher reqD = context.getRequestDispatcher("/para");
reqD.forward(req,resp);// 调用forward实现请求转发
}

读取资源文件

Properties

  • 在Java目录下新建Properties
  • 在resources下新建properties

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

129

读取properties资源文件

1
2
3
4
5
6
7
8
9
10
11
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
// 读取properties资源文件
InputStream is = context.getResourceAsStream("/WEB-INF/classes/db.properties");
Properties pro = new Properties();
pro.load(is);
String user = pro.getProperty("username");
String pwd = pro.getProperty("password");
resp.getWriter().print(user+":"+pwd);
}

web.xml配置

1
2
3
4
5
6
7
8
9
<!--    注册Servlet-->
<servlet>
<servlet-name>pro</servlet-name>
<servlet-class>com.jokerdig.servlet.PropertiesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pro</servlet-name>
<url-pattern>/pro</url-pattern>
</servlet-mapping>