一。ServletContext对象
1.ServletContext概述
ServletContext 是一个容器(域对象)可以存储键值对数据( String key,Object value ),保存在 ServletContext 中的数据不仅可以提供给所有的 servlet 使用,而且可以在整个项目范围内使用(有些框架的过滤器、监听器也可以使用ServletContext )。web容器(tomcat)在启动时,它会为每个web项目承建一个对应的ServletContext对象它代表:当前web项目
作用:
域对象(共享数据)
获取资源在服务器的真实地址
获取全局的配置参数
获取文件MIME类型
2.获取ServletContext对象
1. 通过request对象获得
ServletContext sc = request.getServletContext();
2. 继承HttpServlet后,可以直接调用
ServletContext sc = getServletContext();
3.域对象(共享数据)
在同一个项目范围内,共享数据(多个servlet都可以获取)
相关APi
1. 存储数据
void setAttribute(String name,Object value)
2. 获取数据
Object getAttribute(String name)
3. 删除数据
void removeAttribute(String name)
0. ServletContext getServletContext()
获取ServletContext对象
1. void setAttribute(String name, Object object)
往servletcontext容器中存入数据,name为数据名称,object为数据的值
2. Object getAttribute(String name)
从ServletContext中获取数据,根据指定的数据名称
3. void removeAttribute(String name)
从ServletContext中移除数据,根据指定的数据名称
@WebServlet("/OneServlet")
public class OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 向servletContext域存数据....
ServletContext sc1 = request.getServletContext();
ServletContext sc2 = getServletContext();
sc1.setAttribute("user", "ack");
System.out.println("OneServlet存了数据。。。");
}
}
@WebServlet("/TwoServlet")
public class TwoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 从servletContext域获取数据
String user = (String) request.getServletContext().getAttribute("user");
System.out.println("TwoServlet获取数据:"+user);
}
}
4.ServletContext生命周期
1. 何时创建?
项目加载时,创建
2. 何时销毁?
项目卸载时,销毁3. 作用范围?
与项目共存亡(多个servlet都可以操作它)保存在 ServletContext 中的数据是项目全局共享的数据
5.获取资源在服务器的真实地址
相关api
1. getRealPath(String path)
获取WEB项目的资源文件路径
2. getResourceAsStream(String path)
根据WEB项目的文件获取流
3. getInitParameter(String name)
获取初始化参数
可实现web项目的移植型,动态获取文件真实路径
@WebServlet("/RealpathServlet")
public class RealpathServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取c.jpg 文件真实路径
String carPath = request.getServletContext().getRealPath("/img/c.jpg");
System.out.println(carPath);
// 获取web.xml 文件真实路径
String webPath = request.getServletContext().getRealPath("/WEB-INF/web.xml");
System.out.println(webPath);
}
}
6.获取全局配置参数
读取web.xml配置文件中<context-param>标签信息,实现参数和代码的解耦(多个servlet都可以获取)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--全局配置参数:所有的servlet