java官方文档中这样描述ServletContext:
- There is one context per "web application" per Java Virtual Machine.
这意味着:每一个web应用在每一个JVM中只有一个 ServletContext (上下文)。
Several Web Apps on one Server (one JVM):
- Web App A - Server/JVM 1 - Context 1
- Web App B - Server/JVM 1 - Context 2
- Web App C - Server/JVM 1 - Context 3
Same Web App on several Servers (several JVMs):
- Web App A - Server/JVM 1 - Context 1
- Web App B - Server/JVM 1 - Context 2
- Web App C - Server/JVM 1 - Context 3
- Web App C - Server/JVM 2 - Context 4
当一个应用被部署(deploying)时,ServletContext将会被创建。一旦ServletContext 被创建,它可以被同一个应用下的所有其他servlets和jsp文件共享。在web应用中,ServletContext 也被当做应用级别的变量( called as the application scope variables)。
ServletContextListener 有以下两个方法:
- public void contextInitialized(ServletContextEvent event)
- public void contextDestroyed(ServletContextEvent event)
以下例子创建一个继承ServletContextListener 的类MyAppContextListener ,它实现了ServletContextListener 中的两个方法。
- package com.javaer.tools.config;
- import java.io.File;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- public class MyAppContextListener implements ServletContextListener {
- // NOTE: static references are not a great idea, shown here for simplicity
- public static File xsltFolder;
- public static File xmlIndentXsltFile;
- ServletContext context;
- public void contextInitialized(ServletContextEvent e) {
- xsltFolder = new File(e.getServletContext().getInitParameter("xsltFolder"));
- xmlIndentXsltFile = new File(xsltFolder, "xml-indent.xslt");
-
- context = e.getServletContext();
- // set variable to servlet context
- context.setAttribute("TEST", "TEST_VALUE");
-
- System.out.println("xsltFolder:"+xsltFolder);//xsltFolder:\WEB-INF\xslt
- System.out.println("TEST:"+context.getAttribute("TEST"));//TEST:TEST_VALUE
- }
- @Override
- public void contextDestroyed(ServletContextEvent arg0) {
- //当web服务器shut down或者crash时执行
- }
- }
上面的程序读取了web.xml文档中的以下内容:
- <context-param>
- <param-name>xsltFolder</param-name>
- <param-value>/WEB-INF/xslt</param-value>
- </context-param>
为了让应用部署时加载这个类,我们需要在web.xml中添加以下内容:
- <listener>
- <listener-class>com.javaer.tools.config.MyAppContextListener</listener-class>
- </listener>
完整的web.xml如下(与本例无关内容已经注释):
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="" xmlns="" xmlns:web="/web-app_2_5.xsd" xsi:schemaLocation=" /web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>javer</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- </welcome-file-list>
- <!--
- <servlet>
- <servlet-name>mvc-dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mvc-dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/servlet-context.xml</param-value>
- </context-param>
- -->
- <context-param>
- <param-name>xsltFolder</param-name>
- <param-value>/WEB-INF/xslt</param-value>
- </context-param>
- <!--
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- -->
- <listener>
- <listener-class>com.javaer.tools.config.MyAppContextListener</listener-class>
- </listener>
- </web-app>
当应用deploy时就会输出:
- xsltFolder:\WEB-INF\xslt
- TEST:TEST_VALUE
阅读(2860) | 评论(0) | 转发(0) |