web应用之监听器
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:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"> <!-- 配置 Struts2 的 Filter -->
<welcome-file-list> <!-- <welcome-file>index.jsp</welcome-file> 欢迎页改为MyTag.jsp -->
<welcome-file>MyTag.jsp</welcome-file>
</welcome-file-list> <listener>
<listener-class>com.log.service.WylSessionAttrbuteLister</listener-class>
</listener> <listener>
<listener-class>com.log.service.WylSessionListener</listener-class>
</listener>
<!-- ServletRequestListener -->
<listener>
<listener-class>com.log.service.WylServletRequestListener</listener-class>
</listener> <listener>
<listener-class>com.log.service.WylContextListener</listener-class>
</listener> <servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<!--下面的配置freemarke的ftl文件的位置 -->
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<!-- 是否和服务器(tommcat)一起启动。0为不。1为是 -->
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping> <servlet>
<!-- define a JspSupportServlet Object -->
<servlet-name>JspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<!-- setting JspSupportServlet auto start -->
<load-on-startup>1</load-on-startup>
</servlet> <!-- <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern>
</filter-mapping> -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 我的自定义过滤器 --> <filter>
<filter-name>wylFiler3</filter-name>
<filter-class>com.test.javaAPI.servlet.MyFilter3</filter-class>
</filter> <filter-mapping>
<filter-name>wylFiler3</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<filter-name>wylFiler4</filter-name>
<filter-class>com.test.javaAPI.servlet.MyFilterXX</filter-class>
</filter> <filter-mapping>
<filter-name>wylFiler4</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<filter-name>wylFiler1</filter-name>
<filter-class>com.test.javaAPI.servlet.MyFilter1</filter-class>
</filter> <filter-mapping>
<filter-name>wylFiler1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<filter-name>wylFiler2</filter-name>
<filter-class>com.test.javaAPI.servlet.MyFilter2</filter-class>
</filter> <filter-mapping>
<filter-name>wylFiler2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 测试servlet用的,20161004 -->
<servlet>
<servlet-name>testMy</servlet-name>
<servlet-class>com.test.javaAPI.servlet.MyServlet</servlet-class> </servlet> <servlet-mapping>
<servlet-name>testMy</servlet-name>
<url-pattern>*.xxx</url-pattern>
</servlet-mapping> <!-- 测试servlet用的,20161004 -->
<servlet>
<servlet-name>testMy2</servlet-name>
<servlet-class>com.test.javaAPI.servlet.MyServlet2</servlet-class>
<!-- 设置 web容器已启动就实例化这个servlet,servlet一旦实例化就会调用初始化方法init(),而且生命周期里只会实例化一次 -->
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>testMy2</servlet-name>
<url-pattern>*.yyy</url-pattern>
</servlet-mapping>
<!-- 可以通过通过监听器来获取到,当然了,需要实现ServletContextListener接口 ,
见:com.log.service.WylContextListener-->
<context-param>
<param-name>ctxName</param-name>
<param-value>我是ctx的值</param-value>
</context-param> </web-app>
如下图,已经获取到web.xml中的<context-param>标签中设置的值了,


web应用之监听器的更多相关文章
- web.xml中监听器配置
<!-- 监听器的配置:监听器配置完以后,应用系统在启动的时候就会开启这些监听器. 监听器的理解:监听器好比一个卫兵,卫兵一直站在那里等待长官的命令,当卫兵收到长官的命令以后,立即执行 之前已经 ...
- Web中的监听器【Listener】
Servlet监听器:Servlet规范中定义的一种特殊类,它用于监听Web应用程序中的ServletContext.HttpSession和ServletRequest等域对象的创建与销毁事件,以及 ...
- Web中的监听器【Listener】与过滤器【Filter】 实例
监听器实例: package com.gwssi.listener; import javax.servlet.http.HttpSession; import javax.servlet.http. ...
- java Web三大组件--监听器
监听器概述 监听器(Listener)是一种特殊的Servlet技术,它可以监听Web应用的上下文信息.Servlet请求信息和Servlet会话信息,即ServletContext.ServletR ...
- web.xml中监听器如何顺序加载
最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口, 并且在此自定义监听器中需要用到Spring的依赖注入功 ...
- Web过滤器和监听器
1.过滤器 1.1什么是过滤器 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servle ...
- web.xml上监听器作用
<!--Spring ApplicationContext 载入 --> <listener> <listener-class>org.springframewor ...
- 启动web项目,监听器、过滤器、拦截器启动顺序
启动顺序:监听器 > 过滤器 > 拦截器 记忆技巧:接到命令,监听电报,过滤敌情,拦截行动.
- srpingboot web - 启动(3) 监听器
接上一篇 一. getRunListeners() 在run() 方法中调用了 getRunListeners(args) 方法, 先看一下这个方法干了什么 private SpringApplica ...
随机推荐
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- javascript获取url地址问好后面的值,包括问号
javascript获取url地址问好后面的值,包括问号 <!DOCTYPE html> <html lang="en"> <head> < ...
- css学习笔记二
下面来总结一下盒子模型,流式布局,浮动布局,层布局(定位布局). 1.盒子模型 有二种:IE盒子模型 和 标准w3c盒子模型 1)IE的盒子模型的content包含了padding和border 2) ...
- Ubuntu小私房(3)--Uubutnu启动美化大变身
Grub是什么? GNU GRUB 和GRUB是GRand Unified Bootloader的缩写,它是一个多重操作系统启动管理器.用来引导不同系统,如windows,linux.GRUB是多启动 ...
- Android View动画
Animation TypeEvaluator View的animate方法 ValueAnimator ObjectAnimator AnimatorSet 使用xml来创建动画 animation ...
- android 强制设置横屏 判断是横屏还是竖屏
判断activity 是横屏还是竖屏 方法 1: //根据设备配置信息 Configuration cf= this.getResources().getConfiguration(); //获取设 ...
- servlet生成随机验证码
package com.cgyue; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import jav ...
- tomcat 产生heapdump文件配置
连接地址:http://www.blogjava.net/zhanglongsr/articles/396607.html
- elmah - Error Logging Modules and Handlers for ASP.NET - 1 : 初体验
elmah(英文):https://code.google.com/p/elmah/ 写作思路:先看结果,然后再说原理 elmah文章基本内容如下 1.安装 2.基本使用 3.详细配置讲解 ...
- SQL Server 2012学习笔记 2 Server Core中命令行安装SQL
Setup.exe /qs /ACTION=Install /FEATURES=SQLEngine,Replication /INSTANCENAME=MSSQLSERVER /SQLSVCACCOU ...