package com.log.service; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import com.util.UtilPrint; /** * 对应慕课网 3-1,http://www.imooc.com/video/5658, * ServletContextListener的主要用途: * 1 定时器 * 2 获取全局属性对象(设置全局属性对象) * 3 获取web.xml中的初始化参数 * @author Wei * @time 2016年10月4日 下午8:38:37 */ public class WylContextListener implements ServletContextListener { public WylContextListener() { System.out.println("com.log.service.WylContextListener()实例化,"); } @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("com.log.service.WylContextListener.contextInitialized()...,"); ServletContext sc = sce.getServletContext(); /** * 设置全局变量,然后再整个应用的生命周期里都可以获取到 */ sc.setAttribute("theGlobalPara", "我是全局的属性对象,"); Enumeration<String> initparaNames = sc.getInitParameterNames(); if (initparaNames.hasMoreElements()) { String name = initparaNames.nextElement(); /** * 获取web.xml中的初始化参数, * <context-param> * <param-name>ctxName</param-name> * <param-value>我是ctx的值</param-value> * </context-param> */ String initValue = sc.getInitParameter(name); UtilPrint.printWithSeparatorAndClass("------name:" + name + ",initValue:" + initValue, WylContextListener.class); } else { UtilPrint.printWithSeparatorAndClass("com.log.service.WylContextListener.contextInitialized()...,没有获取到初始化参数", WylContextListener.class); } } @Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("com.log.service.WylContextListener.contextDestroyed()...,"); } }
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 7 8 <!-- 配置 Struts2 的 Filter --> 9 <welcome-file-list> 10 11 <!-- <welcome-file>index.jsp</welcome-file> 欢迎页改为MyTag.jsp --> 12 <welcome-file>MyTag.jsp</welcome-file> 13 </welcome-file-list> 14 15 <listener> 16 <listener-class>com.log.service.WylSessionAttrbuteLister</listener-class> 17 </listener> 18 19 <listener> 20 <listener-class>com.log.service.WylSessionListener</listener-class> 21 </listener> 22 <!-- ServletRequestListener --> 23 <listener> 24 <listener-class>com.log.service.WylServletRequestListener</listener-class> 25 </listener> 26 27 <listener> 28 <listener-class>com.log.service.WylContextListener</listener-class> 29 </listener> 30 31 <servlet> 32 <servlet-name>freemarker</servlet-name> 33 <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class> 34 <!--下面的配置freemarke的ftl文件的位置 --> 35 <init-param> 36 <param-name>TemplatePath</param-name> 37 <param-value>/</param-value> 38 </init-param> 39 <!-- 是否和服务器(tommcat)一起启动。0为不。1为是 --> 40 <load-on-startup>1</load-on-startup> 41 </servlet> 42 43 <servlet-mapping> 44 <servlet-name>freemarker</servlet-name> 45 <url-pattern>*.ftl</url-pattern> 46 </servlet-mapping> 47 48 49 <servlet> 50 <!-- define a JspSupportServlet Object --> 51 <servlet-name>JspSupportServlet</servlet-name> 52 <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class> 53 <!-- setting JspSupportServlet auto start --> 54 <load-on-startup>1</load-on-startup> 55 </servlet> 56 57 58 <!-- <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 59 </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> 60 </filter-mapping> --> 61 <filter> 62 <filter-name>struts2</filter-name> 63 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 64 </filter> 65 66 <filter-mapping> 67 <filter-name>struts2</filter-name> 68 <url-pattern>/*</url-pattern> 69 </filter-mapping> 70 71 <!-- 我的自定义过滤器 --> 72 73 74 <filter> 75 <filter-name>wylFiler3</filter-name> 76 <filter-class>com.test.javaAPI.servlet.MyFilter3</filter-class> 77 </filter> 78 79 <filter-mapping> 80 <filter-name>wylFiler3</filter-name> 81 <url-pattern>/*</url-pattern> 82 </filter-mapping> 83 84 <filter> 85 <filter-name>wylFiler4</filter-name> 86 <filter-class>com.test.javaAPI.servlet.MyFilterXX</filter-class> 87 </filter> 88 89 <filter-mapping> 90 <filter-name>wylFiler4</filter-name> 91 <url-pattern>/*</url-pattern> 92 </filter-mapping> 93 94 95 <filter> 96 <filter-name>wylFiler1</filter-name> 97 <filter-class>com.test.javaAPI.servlet.MyFilter1</filter-class> 98 </filter> 99 100 <filter-mapping> 101 <filter-name>wylFiler1</filter-name> 102 <url-pattern>/*</url-pattern> 103 </filter-mapping> 104 105 <filter> 106 <filter-name>wylFiler2</filter-name> 107 <filter-class>com.test.javaAPI.servlet.MyFilter2</filter-class> 108 </filter> 109 110 <filter-mapping> 111 <filter-name>wylFiler2</filter-name> 112 <url-pattern>/*</url-pattern> 113 </filter-mapping> 114 115 116 <!-- 测试servlet用的,20161004 --> 117 <servlet> 118 <servlet-name>testMy</servlet-name> 119 <servlet-class>com.test.javaAPI.servlet.MyServlet</servlet-class> 120 121 </servlet> 122 123 <servlet-mapping> 124 <servlet-name>testMy</servlet-name> 125 <url-pattern>*.xxx</url-pattern> 126 </servlet-mapping> 127 128 129 <!-- 测试servlet用的,20161004 --> 130 <servlet> 131 <servlet-name>testMy2</servlet-name> 132 <servlet-class>com.test.javaAPI.servlet.MyServlet2</servlet-class> 133 <!-- 设置 web容器已启动就实例化这个servlet,servlet一旦实例化就会调用初始化方法init(),而且生命周期里只会实例化一次 --> 134 <load-on-startup>1</load-on-startup> 135 </servlet> 136 137 <servlet-mapping> 138 <servlet-name>testMy2</servlet-name> 139 <url-pattern>*.yyy</url-pattern> 140 </servlet-mapping> 141 <!-- 可以通过通过监听器来获取到,当然了,需要实现ServletContextListener接口 , 142 见:com.log.service.WylContextListener--> 143 <context-param> 144 <param-name>ctxName</param-name> 145 <param-value>我是ctx的值</param-value> 146 </context-param> 147 148 </web-app>
如下图,已经获取到web.xml中的<context-param>标签中设置的值了,