eclipse进行演示:

1.创建 Dynamic Web Project ,Dynamic Web module version选择3.0

2.在自动生成 的web.xml配置,增加 metadata-complete="false"

<?xml version="1.0" encoding="UTF-8"?>
<javaee:web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://Java.sun.com/xml/ns/javaee"
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false" version="3.0">
<javaee:display-name></javaee:display-name>
<javaee:welcome-file-list>
<javaee:welcome-file>index.html</javaee:welcome-file>
</javaee:welcome-file-list>
</javaee:web-app>

3.创建监听类,在监听类头部增加 注解 @WebListener

package com;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("MyServletContextListener销毁");
} @Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("MyServletContextListener初始化");
System.out.println(sce.getServletContext().getServerInfo());
} }

4.启动tomcat服务。打印结果如下

注意事项,每次修改配置或者java代码后,要重新编译,否则不起作用

eclipse进行演示:

代码如下

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent; @WebListener()
public class Listener2 implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener { // Public constructor is required by servlet spec
public Listener2() {
} // -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is
initialized(when the Web application is deployed).
You can initialize servlet context related data here.
*/
System.out.println("开始初始化");
} public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
System.out.println("开始销毁");
} // -------------------------------------------------------
// HttpSessionListener implementation
// -------------------------------------------------------
public void sessionCreated(HttpSessionEvent se) {
/* Session is created. */
} public void sessionDestroyed(HttpSessionEvent se) {
/* Session is destroyed. */
} // -------------------------------------------------------
// HttpSessionAttributeListener implementation
// ------------------------------------------------------- public void attributeAdded(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is added to a session.
*/
} public void attributeRemoved(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is removed from a session.
*/
} public void attributeReplaced(HttpSessionBindingEvent sbe) {
/* This method is invoked when an attibute
is replaced in a session.
*/
}
}

进行启动

停止启动

@WebListener 注解方式实现监听(eclipse和idea)的更多相关文章

  1. @WebListener 注解方式实现监听

    1.创建 Dynamic Web Project ,Dynamic Web module version选择3.0 2.在自动生成 的web.xml配置,增加 metadata-complete=&q ...

  2. oracle 登录数据库时报 无监听 的一种解决方式(监听日志文件达到4g默认上限)

    问题:登录服务器时 报无监听服务 检查步骤: 1.进入sqlplus查看数据库的状态,显示当前数据库的状态为OPEN 脚本:select status from v$Instance; 2.检查数据库 ...

  3. SpringBoot框架(6)--事件监听

    一.场景:类与类之间的消息通信,例如创建一个对象前后做拦截,日志等等相应的事件处理. 二.事件监听步骤 (1)自定义事件继承ApplicationEvent抽象类 (2)自定义事件监听器,一般实现Ap ...

  4. ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听

    对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...

  5. Java ActiveMQ 讲解(二)Spring ActiveMQ整合+注解消息监听

    对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...

  6. android应用锁之监听应用前后台切换方式

    今天在做技术总结,顺便就把知识共享,个人崇尚分享. 通过以下方式来监听是不是发生了应用的前后台切换: 1. android api 10 – 15 通过ActivityManager register ...

  7. 【laravel】Eloquent 模型事件和监听方式

    所有支持的模型事件 在 Eloquent 模型类上进行查询.插入.更新.删除操作时,会触发相应的模型事件,不管你有没有监听它们.这些事件包括: retrieved 获取到模型实例后触发 creatin ...

  8. Spring基于纯注解方式的使用

    经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配 ...

  9. spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情

    <spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...

随机推荐

  1. Murano Weekly Meeting 2016.08.16

    Meeting time: 2016.August.16 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: ...

  2. [TimesTen]TT7001: User authentication failed

    在使用sqldeveloper连接TimesTen一直报[TimesTen][TimesTen 11.2.2.8.0 ODBC Driver][TimesTen]TT7001: User authen ...

  3. java集合常用操作

    收集一些常用集合操作的代码,用于治疗健忘症,:) set转list //构造Map数据 Map<String, String> map = new HashMap<String, S ...

  4. 游戏源码--Unity开源Moba游戏-服务器-客户端完整V1.0

    http://www.manew.com/thread-111658-1-1.html

  5. web部署启动或者运行报错查看日志寻找问题方法

    今天运行一个项目,启动报错,查看日志,只看到了前半段错误日志,根据前半段错误日志差查找原因,找了两个小时,也没有解决掉,最后根据后半段错误日志十分钟定位错误,给解决了,以后出现问题不能急躁,查看完成的 ...

  6. MongoDB windows基础配置及集群搭建

    由于公司业务的发展MSSQL已处于瓶颈.因为没钱买牛逼服务器只能靠软件来实现最大优化了.原来的系统架构如下图:

  7. 拼凑的宿主-host

    开发两年之久,竟然不知道host这个词是什么意思.前些天有幸遇到了,就跟别人请教了.今天理絮一下.总比不知道强吧. 白话来说宿主就是一些框架运行机制运行时需要依赖的平台. 例如web开发常用的IIS, ...

  8. IE9下JQuery发送ajax请求失效

    最近在做项目的时候,测试PC端网页,在IE9下会失效,不能正常的发送POST请求,经过仔细的排查,发现是IE9下JQuery发送ajax存在跨域问题. 目前有两种解决方案:   解决方案一: 设置浏览 ...

  9. js之闭包

    函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个对Array的求和.通常情况下,求和的函数是这样定义的: function sum(arr) { ret ...

  10. jQuery Custom PopUp Window

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...