- 由Servlet容器负责创建,对于每个JavaWeb应用,在启动时,Servlet容器都会创建一个ServletContext对象。它代表当前web应用。
- ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放。request,一个用户可有多个;session,一个用户一个;而servletContext,所有用户共用一个。
- ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。 - servletContext生命周期:
服务器启动的时候,会为每一个web应用创建一个servetContext,服务器里面有多少个web应用机会有创建多少个servlet。
当服务器停止运行的时候servletContext会销毁,或者web工程被删除的时候,会被销毁。
- 在一个web应用中,所有的servlet可以共享servlet里面的数据:
public class servletContext extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String value="aaa";
//通过servletConfig获得servletContext,将数据存入servletContext
this.getServletConfig().getServletContext().setAttribute("data", value);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
servletContext2 可以得到servletContext中的数据
public class servletContext2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String value=(String)this.getServletContext().getAttribute("data");
System.out.println(value);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
- /实际开发中可以在web.xml文件中配置,当web容器启动的时候,初始化参数会自动被封装到servletContext中。
<context-param>
<param-name>data</param-name>
<param-value>HELLO我是初始化参数</param-value>
</context-param>
public class servletContext3 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String value=this.getServletContext().getInitParameter("data");
System.out.println(value);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
- servletContex实现请求转发:当一个servlet无法单独完成客户端的请求时,这时就要用到其他的servlet对请求信息进行处理。
使用方法:public void forward(ServletRequest request, ServletResponse response) throws ServletException,IOException;
public class servletContextDispatcher extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//请求转发
String data="aaa";
this.getServletContext().setAttribute("data", data);
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/1.jsp");
rd.forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
1.jsp
<body>
<font color="red">
<%
String data=(String)application.getAttribute("data");
out.write(data);
%>
</font>
</body>
- servletContext读取资源文件:
资源文件一般有两种:xml里面的数据有关,properties里面的数据无关:
db.properties:
url=jdbc:mysql://localhost:3306/test
username=root
password=root
public class servletContext4 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//注意将工程发布到服务器上以后,没有src文件,为classes文件
InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//所有的properties文件都用properties处理
Properties props=new Properties();
props.load(in);
System.out.println(props.getProperty("url")+","+props.getProperty("username")+","+props.getProperty("password"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}