<context-param>
web.xml的配置项,标识application范围内的参数,加载比其他任何Servlet都早。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- c1-start 配置spring的父上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring.xml,classpath*:spring/applicationContext-per.xml
</param-value>
</context-param>
<!-- 登录失败Url -->
<context-param>
<param-name>failUrl</param-name>
<param-value>/</param-value>
</context-param>
- 启动web容器将读取web.xml上的节点:<listener>、<context-param>;
- 容器将<context-param>转化成值对保存在web容器创建的application内共享的ServletContext中;
- 在ServletContextListener(ContextLoaderListener ) 监听器中contextInitialized( ServletContextEvent sce ) 初始化方法中获得:
servletContext = ServletContextEvent.getServletContext()/** * Implementations of this interface receive notifications about * changes to the servlet context of the web application they are * part of. * To receive notification events, the implementation class * must be configured in the deployment descriptor for the web * application. * @see ServletContextEvent * @since v 2.3 */ public interface ServletContextListener extends EventListener { /** ** Notification that the web application initialization ** process is starting. ** All ServletContextListeners are notified of context ** initialization before any filter or servlet in the web ** application is initialized. */ public void contextInitialized ( ServletContextEvent sce ); /** ** Notification that the servlet context is about to be shut down. ** All servlets and filters have been destroy()ed before any ** ServletContextListeners are notified of context ** destruction. */ public void contextDestroyed ( ServletContextEvent sce ); }
ServletContext
WEB容器启动时会创建一个代表当前web应用的上下文ServletContext对象,通常称为context 域对象。
不同的Web应用,ServletContext各自独立存在。只在Web应用被关闭时才被销毁。
- 获取到整个web应用的配置信息<context-param>
context-param的值 = ServletContext.getInitParameter("context-param的键");
- 实现Servlet间通讯。 主要是4个有关 Attribute 的方法:setAttribute()、getAttribute()、removeAttribute()、getAttributeNames()
ServletContext context =this.getServletContext(); // servletContext域对象 context.setAttribute("data","共享数据"); // 向域中存了一个data属性 // 在另一个servlet中,可以使用如下语句来获取域中的data属性 ServletContext context =this.getServletContext(); String value = (String)context.getAttribute("data"); // 获取域中的data属性
- 读取资源文件
String path= "/WEB-INF/classes/db.properties"; //默认'/webapp'下目录 //返回资源文件的读取字节流 InputStream in =this.getServletContext().getResourceAsStream(path); //获得文件的完整绝对路径path,再使用字节流读取path下的文件 String allPath =this.getServletContext().getRealPath(path); //获得一个url对象,调用该类的openStream方法返回一个字节流,读取数据 URL url=this.getServletContext().getResource(path);
<init-param>
servlet范围内的参数,只在指定的servlet中获取。
<!-- 配置springmvc的前端控制器,同时初始化子上下文 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
init-param的值 = Servlet.getInitParameter("init-param的键");
ServletConfig
在Servlet的配置文件中可多个<init-param>为servlet配置一些初始化参数。web容器创建servlet实例对象时自动将初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。
ServletConfig对象中维护了ServletContext对象的引用: ServletConfig.getServletContext()。