2. 实现多个Servlet对象共享数据
ServletContext接口的方法
方法说明 | 功能描述 |
---|---|
Enumeration getAttributeNames() | 返回一个Enumeration对象,该对象包含了所有存放在ServletContext中的所有域属性名 |
Object getAttibute(String name) | 根据参数指定的属性名返回一个与之匹配的域属性值 |
void removeAttribute(String name) | 根据指定的域属性名,从ServletContext中删除匹配的域属性 |
void setAttribute(String name,Object obj) | 设置ServletContext的域属性,其中name是域属性名,obj是域属性值 |
创建Servlet类TestServlet04和TestServlet05
TestServlet04:
public class TestServlet04 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context=this.getServletContext();
//通过setAttribute()方法设置属性值
context.setAttribute("data", "this servlet save data");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
//浏览器测试网址 http://localhost:8080/chapter03/TestServlet04
}
TestServlet05:
public class TestServlet05 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
ServletContext context=this.getServletContext();
//通过getAttribute()方法获取属性值
String data=(String) context.getAttribute("data");
out.println(data);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
//浏览器测试网址 http://localhost:8080/chapter03/TestServlet05
}
运行结果:
先运行TestServlet04将数据存入ServletContext对象,再运行TestServlet05,得到如下结果:
Servlet数据共享补充
/*
* WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
* ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.
* getServletContext方法获得ServletContext对象。
* 由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,
* 因此Servlet对象之间可以通过ServletContext对象来实现通讯。 ServletContext对象通常也被称之为context域对象。
*
* Servlet1:
* protected void doGet(HttpServletRequest request,
* HttpServletResponse response) throws ServletException, IOException {
* String data = "value";
* ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象
* context.setAttribute("data", data); //将data存储到ServletContext对象中 }
*
* Servlet2:
* protected void doGet(HttpServletRequest request, HttpServletResponse
* response) throws ServletException, IOException {
* ServletContext context = this.getServletContext(); String data = (String)
* context.getAttribute("data");//从ServletContext对象中取出数据
* response.getWriter().print("data="+data); }
* Servlet2拿到的context与Servlet1设置data的context是同一个对象,所以说servlet中this.
* getServletContext(); this.getServletConfig().getServletContext(); 本质上没有区别。
*/