`
zzc1684
  • 浏览: 1190215 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

支持注解的Servlet3.0监听器

阅读更多

支持注解的Servlet3.0监听器

 

       Servlet3.0中的监听器跟之前2.5的差别不大,唯一的区别就是增加了对注解的支持。在3.0以前我们的监听器配置都是需要配置在web.xml文件中的。在3.0中我们有了更多的选择,之前在web.xml文件中配置的方式还是可以的,同时我们还可以使用注解进行配置。对于使用注解的监听器就是在监听器类上使用@WebListener进行标注,这样Web容器就会把它当做一个监听器进行注册和使用了。

 

       在这篇文章中我主要想讲的还是Servlet3.0中可以使用注解来配置监听器,对于监听器的其它内容我不想做过多的涉及。

 

       对于使用注解的监听器我想挑两类型监听器来举一个例子,一类是对Session的监听,一类是对ServletContext的监听。对于Session的监听器主要有HttpSessionListenerHttpSessionAttributeListenerHttpSessionListener可以监听HttpSession的创建跟销毁,而HttpSessionAttributeListener则是对session中属性的监听,它可以监听到session新增属性、移除属性和属性值被替换时。对于ServletContext的监听器有ServletContextListenerServletContextAttributeListenerServletContextListener可以监听到ServletContext的创建和销毁,而ServletContextAttributeListener可以监听到ServletContext中属性的新增、移除和属性值的替换。

Java代码  收藏代码
  1.    
  2. import javax.servlet.annotation.WebListener;  
  3. import javax.servlet.http.HttpSessionAttributeListener;  
  4. import javax.servlet.http.HttpSessionBindingEvent;  
  5. import javax.servlet.http.HttpSessionEvent;  
  6. import javax.servlet.http.HttpSessionListener;  
  7.    
  8. /** 
  9.  * 
  10.  * HttpSession监听器和HttpSession属性监听器 
  11.  * 
  12.  */  
  13. @WebListener  
  14. public class SessionListener implements HttpSessionAttributeListener,  
  15.        HttpSessionListener {  
  16.    
  17.     @Override  
  18.     public void sessionCreated(HttpSessionEvent se) {  
  19.        System.out.println("session created");  
  20.     }  
  21.    
  22.     @Override  
  23.     public void sessionDestroyed(HttpSessionEvent se) {  
  24.        System.out.println("session destroyed");  
  25.     }  
  26.    
  27.     @Override  
  28.     public void attributeAdded(HttpSessionBindingEvent event) {  
  29.        System.out.println("session attribute added");  
  30.     }  
  31.    
  32.     @Override  
  33.     public void attributeRemoved(HttpSessionBindingEvent event) {  
  34.        System.out.println("session attribute removed");  
  35.     }  
  36.    
  37.     @Override  
  38.     public void attributeReplaced(HttpSessionBindingEvent event) {  
  39.        System.out.println("session attribute replaced");  
  40.     }  
  41.    
  42. }  

 

Java代码  收藏代码
  1.    
  2. import javax.servlet.ServletContextAttributeEvent;  
  3. import javax.servlet.ServletContextAttributeListener;  
  4. import javax.servlet.ServletContextEvent;  
  5. import javax.servlet.ServletContextListener;  
  6. import javax.servlet.annotation.WebListener;  
  7.    
  8. /** 
  9.  * 
  10.  * ServletContext监听器和ServletContext属性监听器 
  11.  * 
  12.  */  
  13. @WebListener  
  14. public class ContextListener implements ServletContextAttributeListener,  
  15.        ServletContextListener {  
  16.    
  17.     @Override  
  18.     public void contextDestroyed(ServletContextEvent sce) {  
  19.        System.out.println("ServletContext destroyed");  
  20.     }  
  21.    
  22.     @Override  
  23.     public void contextInitialized(ServletContextEvent sce) {  
  24.        System.out.println("ServletContext initialized");  
  25.     }  
  26.    
  27.     @Override  
  28.     public void attributeAdded(ServletContextAttributeEvent event) {  
  29.        System.out.println("ServletContext attribute added");  
  30.     }  
  31.    
  32.     @Override  
  33.     public void attributeRemoved(ServletContextAttributeEvent event) {  
  34.        System.out.println("ServletContext attribute removed");  
  35.     }  
  36.    
  37.     @Override  
  38.     public void attributeReplaced(ServletContextAttributeEvent event) {  
  39.        System.out.println("ServletContext attribute replaced");  
  40.     }  
  41.    
  42. }  

 

       为了文章的完整性,下面将给出上面监听器对应的在web.xml中配置的方式。

Xml代码  收藏代码
  1. <listener>  
  2.    <listener-class>com.xxx.SessionListener</listener-class>  
  3. </listener>  
  4. <listener>  
  5.    <listener-class>com.xxx.ContextListener</listener-class>  
  6. </listener>  

 

分享到:
评论

相关推荐

    annotaction

    2. 新增的注解支持:该版本新增了若干注解,用于简化 Servlet、过滤器(Filter)和监听器(Listener)的声 明,这使得 web.xml 部署描述文件从该版本开始不再是必选的了。 3. 可插性支持:熟悉 Struts2 的开发者一定...

    springweb3.0MVC注解(附实例)

    -- Spring 容器启动监听器 --&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener&gt; &lt;!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC...

    Java™ Servlet 规范.

    1.6.1 监听器(Listener)顺序 ...............................................................................................................14 1.6.2 注解处理 .............................................

    通俗易懂的Spring注解驱动开发教程(含配套资料)

    本教程为授权出品教程 《Spring注解驱动开发》是一套帮助我们深入了解Spring原理... 3).web原理 1).Servlet3.0标准新增特性 2).异步请求相关 本视频使用了maven构建程序,需要同学们有对Spring.SpringMVC的基本

    Servlet3.1规范(最终版) PDF

    1.6.1 监听器(Listener)顺序 ...............................................................................................................14 1.6.2 注解处理 ...............................................

    spring-annotion:Spring源码分析以及注解开发

    Spring注解驱动开发,包含一个refresh()方法的流程(在resources目录下) 模块介绍 ---&gt; ext扩展原理包含beanFactoryPostProcessor,...启动也包含SpringMVC注解化开发和Servlet3.0注解开发

    springboot学习思维笔记.xmind

    定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware BeanNameAware BeanFactoryAware ApplicationContextAware MessageSourceAware ...

    Spring.3.x企业应用开发实战(完整版).part2

    12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务处理 12.2.8 延迟加载的问题 12.3 在Spring中使用myBatis 12.3.1 配置SqlMapClient 12.3.2 在Spring配置myBatis ...

    Spring3.x企业应用开发实战(完整版) part1

    12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务处理 12.2.8 延迟加载的问题 12.3 在Spring中使用myBatis 12.3.1 配置SqlMapClient 12.3.2 在Spring配置myBatis ...

    Spring攻略(第二版 中文高清版).part2

    6.2 在你的Servlet和过滤器中使用Spring 214 6.2.1 问题 214 6.2.2 解决方案 215 6.2.3 工作原理 215 6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4...

    Spring攻略(第二版 中文高清版).part1

    6.2 在你的Servlet和过滤器中使用Spring 214 6.2.1 问题 214 6.2.2 解决方案 215 6.2.3 工作原理 215 6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4...

Global site tag (gtag.js) - Google Analytics