服务器会为每个应用创建一个ServletContext对象:
ServletContext对象的创建是在服务器启动时完成的;
ServletContext对象的销毁是在服务器关闭时完成的。
ServletContext对象的作用是在整个Web应用的动态资源之间共享数据!例如在AServlet中向ServletContext对象中保存一个值,然后在BServlet中就可以获取这个值,这就是共享数据了。
ServletConfig#getServletContext();
GenericServlet#getServletContext();
HttpServlet#getServletContext()
ServletContextEvent#getServletContext()
在Servlet中获取ServletContext对象:
在void init(ServletConfig config)中:
ServletContext context = config.getServletContext();
ServletConfig类的getServletContext()方法可以用来获取ServletContext对象;
public class MyServlet implements Servlet {
public void init(ServletConfig config) {
ServletContext context = config.getServletContext();
}
…
}
在GenericeServlet或HttpServlet中获取ServletContext对象:
GenericServlet类有getServletContext()方法,所以可以直接使用 this.getServletContext()来获取;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
ServletContext context = this.getServletContext();
}
}
域对象就是用来在多个Servlet中传递数据!!!
域对象必须有要存数据功能
域对象必须要有取数据功能
域对象内部其实有一个Map
ServletContext是JavaWeb四大域对象之一:
PageContext;
ServletRequest;
HttpSession;
ServletContext;
所有域对象都有存取数据的功能,因为域对象内部有一个Map,用来存储数据,下面是ServletContext对象用来操作数据的方法:
void setAttribute(String name, Object value)
用来存储一个对象,也可以称之为存储一个域属性,
例如:
servletContext.setAttribute(“xxx”, “XXX”)
在ServletContext中保存了一个域属性,域属性名称为xxx,域属性的值为XXX。请注意,如果多次调用该方法,并且使用相同的name,那么会覆盖上一次的值,这一特性与Map相同;
Object getAttribute(String name)
用来获取ServletContext中的数据,当前在获取之前需要先去存储才行,
例如:
String value = (String)servletContext.getAttribute(“xxx”);,
获取名为xxx的域属性;
void removeAttribute(String name)
用来移除ServletContext中的域属性,如果参数name指定的域属性不存在,那么本方法什么都不做;
Enumeration getAttributeNames()
获取所有域属性的名称;
用一个Servlet存值到ServletContext中,用另外一个Servlet取值
public class AServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public AServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
context.setAttribute("zhangsan", "18");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
public class BServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String value = (String) context.getAttribute("zhangsan");
System.out.println(value);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlet也可以获取初始化参数,但它是局部的参数。
也就是说,一个Servlet只能获取自己的初始化参数,不能获取别人的,即初始化参数只为一个Servlet准备!
可以配置公共的初始化参数,为所有Servlet而用!但这需要使用ServletContext才能使用!
可以使用ServletContext来获取在web.xml文件中配置的应用初始化参数!
注意,应用初始化参数与Servlet初始化参数不同!
<context-param>
<param-name>p1</param-name>
<param-value>v1</param-value>
</context-param>
<context-param>
<param-name>p2</param-name>
<param-value>v2</param-value>
</context-param>
ServletContext context = this.getServletContext();
String value1 = context.getInitParameter("p1");
String value2 = context.getInitParameter("p2");
System.out.println(value1 + ", " + value2);
Enumeration names = context.getInitParameterNames();
while(names.hasMoreElements()) {
System.out.println(names.nextElement());
}