eclipse进行演示:

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

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

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

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

  1. package com;
    import javax.servlet.ServletContextEvent;
  2. import javax.servlet.ServletContextListener;
  3. import javax.servlet.annotation.WebListener;
  4. @WebListener
  5. public class MyServletContextListener implements ServletContextListener {
  6. @Override
  7. public void contextDestroyed(ServletContextEvent sce) {
  8. System.out.println("MyServletContextListener销毁");
  9. }
  10.  
  11. @Override
  12. public void contextInitialized(ServletContextEvent sce) {
  13. System.out.println("MyServletContextListener初始化");
  14. System.out.println(sce.getServletContext().getServerInfo());
  15. }
  16.  
  17. }

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

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

eclipse进行演示:

代码如下

  1. import javax.servlet.ServletContextEvent;
  2. import javax.servlet.ServletContextListener;
  3. import javax.servlet.annotation.WebListener;
  4. import javax.servlet.http.HttpSessionAttributeListener;
  5. import javax.servlet.http.HttpSessionEvent;
  6. import javax.servlet.http.HttpSessionListener;
  7. import javax.servlet.http.HttpSessionBindingEvent;
  8.  
  9. @WebListener()
  10. public class Listener2 implements ServletContextListener,
  11. HttpSessionListener, HttpSessionAttributeListener {
  12.  
  13. // Public constructor is required by servlet spec
  14. public Listener2() {
  15. }
  16.  
  17. // -------------------------------------------------------
  18. // ServletContextListener implementation
  19. // -------------------------------------------------------
  20. public void contextInitialized(ServletContextEvent sce) {
  21. /* This method is called when the servlet context is
  22. initialized(when the Web application is deployed).
  23. You can initialize servlet context related data here.
  24. */
  25. System.out.println("开始初始化");
  26. }
  27.  
  28. public void contextDestroyed(ServletContextEvent sce) {
  29. /* This method is invoked when the Servlet Context
  30. (the Web application) is undeployed or
  31. Application Server shuts down.
  32. */
  33. System.out.println("开始销毁");
  34. }
  35.  
  36. // -------------------------------------------------------
  37. // HttpSessionListener implementation
  38. // -------------------------------------------------------
  39. public void sessionCreated(HttpSessionEvent se) {
  40. /* Session is created. */
  41. }
  42.  
  43. public void sessionDestroyed(HttpSessionEvent se) {
  44. /* Session is destroyed. */
  45. }
  46.  
  47. // -------------------------------------------------------
  48. // HttpSessionAttributeListener implementation
  49. // -------------------------------------------------------
  50.  
  51. public void attributeAdded(HttpSessionBindingEvent sbe) {
  52. /* This method is called when an attribute
  53. is added to a session.
  54. */
  55. }
  56.  
  57. public void attributeRemoved(HttpSessionBindingEvent sbe) {
  58. /* This method is called when an attribute
  59. is removed from a session.
  60. */
  61. }
  62.  
  63. public void attributeReplaced(HttpSessionBindingEvent sbe) {
  64. /* This method is invoked when an attibute
  65. is replaced in a session.
  66. */
  67. }
  68. }

进行启动

停止启动

@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. Ace教你一步一步做Android新闻客户端(四) 优化Bitmap大法

    我计划着把需要用到的知识分解开来写,趁着我们要开发这款客户端的机会把安卓所有移动客户端开发中的技术贯穿其中,也是我自己成长的过程.By Ace in 20160121 我们开发一款新闻客户端程序,它的 ...

  2. 数据库和AI的一次火花

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由宗文 发表于云+社区专栏 | 导语 通过历史数据,基于时间序列来预测未来. 我们生活中很多数据是有时间维度的.比如说天气或者股票价格. ...

  3. Beyond Compare 4试用期已过

    Beyond Compare 很好用,但是只有一段时间的试用时间,当试用期过了之后就提示不能试用了 怎么办呢? 我在网上找到了两个方法: 1.直接用注册码(来自:https://blog.csdn.n ...

  4. HRBUST 1909——理工门外的树——————【离线处理,差分前缀和】

    理工门外的树 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class n ...

  5. jquery的$.getScript在IE下的缓存问题

    jquery的$.getScript在IE下的缓存问题

  6. Django——stark组件

    stark组件是仿照django的admin模块开发的一套组件,它的作用是在网页上对注册的数据表进行增删改查操作. 一.配置 1.创建stark应用,在settings.py中注册stark应用 st ...

  7. vue 音乐播放器报错

    使用Vue报错[Vue warn]: Error in nextTick: "TypeError: fn.bind is not a function"页面进不去. 检查:看看da ...

  8. jsonp跨域&百度下拉

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. js添加、修改、删除xml节点例子

    version="1.0" encoding="gb2312"?> . <bookstore> . <book genre=" ...

  10. 001Angular2环境准备

    01.安装Node.js 和 npm 安装完成后,在命令行依次使用node -v 和 npm -v可以看到版本号. 02.安装cnpm npm需要连接谷歌服务器,国内不能正常使用,需安装cnpm.命令 ...