2.4.3 域对象
JavaWeb四个域对象:(1) PageContext;(2) ServletRequest;(3) HttpSession;(4) ServletContext。每个域对象都有数据存取功能(因为内部都有一个Map),下面是ServletContext对象用来操作数据的方法:
(1) void setAttribute(String name, Object value):用来存储一个对象,也可以称之为存储一个域属性,例如:setAttribute("xx", "XXX"),在ServletContext中保存了一个域属性,域属性名称为xx,域属性的值为XXX。请注意,如果多次调用该方法,并且使用相同的name,那么会覆盖上一次的值,这一特性与Map相同;
(2) Object getAttribute(String name):用来获取ServletContext中的数据,当然在获取之前需要先去存储才行。例如:String value = (String) servletContext.getAttribute("xx");获取名为xx的域属性值;
(3) void removeAttribute(String name):用来移除ServletContext中的域属性,如果参数name指定的域属性不存在,那么本方法什么都不做;
(4) Enumeration getAttributeNames():获取所有域属性的名称。
演示向ServletContext中保存(获取)数据:
public class BServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application = this.getServletContext();
application.setAttribute("username", "admin123");
application.setAttribute("city", new City("bj", 1001));
}
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application= this.getServletContext();
String username = (String) application.getAttribute("username");
City city = (City) application.getAttribute("city");
System.out.println("username: " + username);
System.out.println(city);
}}
先访问BServlet,再访问AServlet
2.4.4 获取初始化参数
Web应用涉及到多种参数:
(1) 请求参数:浏览器发送请求中的参数;
(2) 域参数:四大域中使用setAttribute()方法设置的参数;
(3) 初始化参数:在web.xml文件中配置的初始化参数;
- 配置在<servlet>…</servlet>元素中的,属于特定的Servlet;
- 配置在<web-apps>…</web-app>中的,属于整个ServletContext。
Servlet可以获取初始化参数,但它是局部的参数,也就是说,一个Servlet只能获取它自己的初始化参数,不能获取别的Servlet设置的参数;也可以在web.xml中配置公共的初始化参数,为所有的Servlet使用,这就需要使用ServletContext对象。
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.zhb.web.servlet.HelloServlet</servlet-class>
<init-param>
<param-name>parameterName1</param-name>
<param-value>parameterValue1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextParameterName1</param-name>
<param-value>contextParameterValue1</param-value>
</context-param>
</web-app>
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*获取HelloServlet自身的初始化参数*/
ServletConfig config = this.getServletConfig();
String servletValue = config.getInitParameter("servletParameterName1");
System.out.println("servlet_init_parameter_value: " + servletValue);
/*获取ServletContext全局的初始化参数*/
ServletContext application = this.getServletContext();
String contextValue = application.getInitParameter("contextParameterName1");
System.out.println("context_init_parameter_value:" + contextValue);
}
}