一、ServletContext
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用
(一)共享数据:不同Servlet中的数据可以共享
(二) 获取初始化参数
<!--配置一些web初始化参数-->
<context-param>
<param-name>hello</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
(三)请求转发
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().println(url);
}
(四)读取资源文件
Properties
- 在java目录下新建properties
- 在resources目录下新建properties
发现都被打包到同一路径下:classes,俗称这个路径为classpath。
思路:需要一个文件流