`
Jameslyy
  • 浏览: 401953 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Servlet Listener 和 Filter

阅读更多

1、Servlet Listener

      共有8个可以实现的监听器接口,和6种相对应的触发事件,实现在触发不同事件时执行相应的操作。如下

java 代码
  1. //   
  2. import javax.servlet.ServletRequestEvent;   
  3. import javax.servlet.ServletRequestListener;   
  4. //   
  5. import javax.servlet.ServletRequestAttributeEvent;   
  6. import javax.servlet.ServletRequestAttributeListener;   
  7. //   
  8. import javax.servlet.ServletContextEvent;   
  9. import javax.servlet.ServletContextListener;   
  10. //   
  11. import javax.servlet.ServletContextAttributeEvent;   
  12. import javax.servlet.ServletContextAttributeListener;   
  13. //   
  14. import javax.servlet.http.HttpSessionEvent;   
  15. import javax.servlet.http.HttpSessionListener;   
  16. import javax.servlet.http.HttpSessionActivationListener;   
  17. //   
  18. import javax.servlet.http.HttpSessionBindingEvent;   
  19. import javax.servlet.http.HttpSessionBindingListener;   
  20. import javax.servlet.http.HttpSessionAttributeListener;  

1)、javax.servlet.ServletContextListener

Implementations of this interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.

void com.james.webapp.ServletListener.contextInitialized(ServletContextEvent arg0)

Notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.

void com.james.webapp.ServletListener.contextDestroyed(ServletContextEvent arg0)

Notification that the servlet context is about to be shut down. All servlets and filters have been destroy()ed before any ServletContextListeners are notified of context destruction.

2)、javax.servlet.ServletContextAttributeListener

Implementations of this interface receive notifications of changes to the attribute list on the servlet context of a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.

java 代码
  1. /** Notification that a new attribute was added to the servlet context.   
  2. Called after the attribute is added.*/     
  3. public void attributeAdded(ServletContextAttributeEvent scab);      
  4. /** Notification that an existing attribute has been removed from the servlet context.   
  5. Called after the attribute is removed.*/     
  6. public void attributeRemoved(ServletContextAttributeEvent scab);      
  7. /** Notification that an attribute on the servlet context has been replaced.   
  8. Called after the attribute is replaced. */     
  9. public void attributeReplaced(ServletContextAttributeEvent scab);    

3)、javax.servlet.ServletRequestListener

A ServletRequestListener can be implemented by the developer interested in being notified of requests coming in and out of scope in a web component. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.

java 代码
  1. /** The request is about to go out of scope of the web application. */  
  2. public void requestDestroyed ( ServletRequestEvent sre );   
  3.   
  4. /** The request is about to come into scope of the web application. */  
  5. public void requestInitialized ( ServletRequestEvent sre );  

4)、javax.servlet.ServletRequestAttributeListener

A ServletRequestAttributeListener can be implemented by the developer interested in being notified of request attribute changes. Notifications will be generated while the request is within the scope of the web application in which the listener is registered. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.

Since:
Servlet 2.4
java 代码
  1. /** Notification that a new attribute was added to the  
  2.   ** servlet request. Called after the attribute is added.  
  3.   */  
  4.  public void attributeAdded(ServletRequestAttributeEvent srae);   
  5.   
  6.  /** Notification that an existing attribute has been removed from the  
  7.   ** servlet request. Called after the attribute is removed.  
  8.   */  
  9.  public void attributeRemoved(ServletRequestAttributeEvent srae);   
  10.   
  11.  /** Notification that an attribute was replaced on the  
  12.   ** servlet request. Called after the attribute is replaced.  
  13.   */  
  14.  public void attributeReplaced(ServletRequestAttributeEvent srae);  

5)、javax.servlet.http.HttpSessionListener

Implementations of this interface are notified of changes to the list of active sessions in a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.

Since:
v 2.3
java 代码
  1. /**     
  2. * Notification that a session was created.    
  3. * @param se the notification event    
  4. */     
  5. public void sessionCreated ( HttpSessionEvent se );      
  6.      
  7. /**     
  8. * Notification that a session is about to be invalidated.    
  9. * @param se the notification event    
  10. */     
  11. public void sessionDestroyed ( HttpSessionEvent se   

6)、javax.servlet.http.HttpSessionActivationListener

Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener.

Since:
2.3
java 代码
  1. /** Notification that the session is about to be passivated.*/  
  2. public void sessionWillPassivate(HttpSessionEvent se);    
  3. /** Notification that the session has just been activated.*/  
  4. public void sessionDidActivate(HttpSessionEvent se);  

7)、javax.servlet.http.HttpSessionBindingListener

Causes an object to be notified when it is bound to or unbound from a session. The object is notified by an HttpSessionBindingEvent object. This may be as a result of a servlet programmer explicitly unbinding an attribute from a session, due to a session being invalidated, or due to a session timing out.

See Also:
HttpSession
HttpSessionBindingEvent
java 代码
  1. /**  
  2.  * Notifies the object that it is being bound to  
  3.  * a session and identifies the session.  
  4.  *  
  5.  * @param event   the event that identifies the session   
  6.  */    
  7. public void valueBound(HttpSessionBindingEvent event);   
  8.   
  9. /**  
  10.  * Notifies the object that it is being unbound  
  11.  * from a session and identifies the session.  
  12.  *  
  13.  * @param event   the event that identifiesthe session   
  14.  */  
  15. public void valueUnbound(HttpSessionBindingEvent event);  

8)、javax.servlet.http.HttpSessionAttributeListener

This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.

Since:
v 2.3
java 代码
  1.     /** Notification that an attribute has been added to a session.   
  2.     Called after the attribute is added.*/  
  3.     public void attributeAdded ( HttpSessionBindingEvent se );   
  4.     /** Notification that an attribute has been removed from a session.   
  5.     Called after the attribute is removed. */  
  6.     public void attributeRemoved ( HttpSessionBindingEvent se );   
  7.     /** Notification that an attribute has been replaced in a session.   
  8.     Called after the attribute is replaced. */  
  9.     public void attributeReplaced ( HttpSessionBindingEvent se );  

 

 2、Servlet Filter

      需要实现接口javax.servlet.Filter,下面是一个例子:

java 代码
  1. import java.io.IOException;   
  2.   
  3. import javax.servlet.Filter;   
  4. import javax.servlet.FilterChain;   
  5. import javax.servlet.FilterConfig;   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.ServletRequest;   
  8. import javax.servlet.ServletResponse;   
  9.   
  10. public class ServerletFilter implements Filter {   
  11.   
  12.     public void destroy() {   
  13.         // TODO Auto-generated method stub   
  14.     }   
  15.   
  16.     public void doFilter(ServletRequest arg0, ServletResponse arg1,   
  17.             FilterChain arg2) throws IOException, ServletException {   
  18.         // TODO Auto-generated method stub   
  19.     }   
  20.   
  21.     public void init(FilterConfig arg0) throws ServletException {   
  22.         // TODO Auto-generated method stub   
  23.     }   
  24.  
  25. }  

javax.servlet.Filter

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.

Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.

Filters are configured in the deployment descriptor of a web application

Examples that have been identified for this design are
1) Authentication Filters
2) Logging and Auditing Filters
3) Image conversion Filters
4) Data compression Filters
5) Encryption Filters
6) Tokenizing Filters
7) Filters that trigger resource access events
8) XSL/T filters
9) Mime-type chain Filter

void com.james.webapp.ServerletFilter.destroy()

Called by the web container to indicate to a filter that it is being taken out of service. This method is only called once all threads within the filter's doFilter method have exited or after a timeout period has passed. After the web container calls this method, it will not call the doFilter method again on this instance of the filter.

This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter's current state in memory.

void com.james.webapp.ServerletFilter.doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

A typical implementation of this method would follow the following pattern:-
1. Examine the request
2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
4. a) Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()),
4. b) or not pass on the request/response pair to the next entity in the filter chain to block the request processing
5. Directly set headers on the response after invocation of the next entity in the filter chain.

void com.james.webapp.ServerletFilter.init(FilterConfig arg0) throws ServletException

Called by the web container to indicate to a filter that it is being placed into service. The servlet container calls the init method exactly once after instantiating the filter. The init method must complete successfully before the filter is asked to do any filtering work.

The web container cannot place the filter into service if the init method either
1.Throws a ServletException
2.Does not return within a time period defined by the web container


分享到:
评论
1 楼 softkid 2008-10-13  
上当了

相关推荐

    servlet+filter+listener 详解

    Servlet、Filter、Listener 详解 Servlet 是运行在服务器上的小程序,它可以使任何 Java 应用程序、...Servlet 负责处理客户端的请求,Filter 负责过滤和拦截 Web 资源,而 Listener 负责监听Servlet 容器中的事件。

    web.xml 中的listener、 filter、servlet 加载顺序及其详解.doc

    一般来说,listener 的加载顺序是在 filter 和 servlet 之前的,因为 listener 需要监听 ServletContext 的事件,以便实现对 Web 应用的监听和控制。filter 的加载顺序是在 servlet 之前的,因为 filter 需要对请求...

    关于web.xml配置文件servlet,filter,listener加载顺序

    关于filter、servlet在web.xml配置及加载顺序

    Servlet、Filter、Listener深入理解.docx

    Servlet、Filter和Listener是Java Web开发中的核心组件,它们在构建动态Web应用程序中扮演着重要角色。下面将分别深入解析这三个概念。 **Servlet接口** Servlet是Java编程语言中定义的一个接口,它允许开发者创建...

    web服务器三大组件servlet、Filter、Listener——浅浅笔记

    Web服务器中的三大组件,即Servlet、Filter和Listener,是构建动态Web应用程序的关键元素。这些组件都是基于Java的,主要用于增强和扩展Web服务器的功能。 Servlet是Java中用于处理HTTP请求的核心组件,它是动态...

    servlet_filter_listener

    `chapter02.docx`可能包含的是关于Servlet、Filter和Listener的第二章内容,详细讲解了这三个组件的实现细节、配置方法以及实际应用案例。而`myblog_v2`可能是一个示例项目,展示了如何在实际的博客系统中运用这些...

    杂记(一):Listener、Filter和工具

    在IT领域,Listener、Filter和工具是Web开发中不可或缺的部分,它们在构建高效、可扩展的应用程序中扮演着重要角色。下面将详细讲解这三个概念及其相关知识点。 首先,Listener(监听器)是Java Servlet规范中的一...

    Servlet中的Filter

    按照配置顺序,先执行Listener,然后是Filter,接着是Struts拦截器,最后是Servlet。 - Filter的执行顺序取决于它们在web.xml中的配置顺序。每个Filter的doFilter()方法会被调用,直到请求达到Servlet,或者在...

    Spring 管理filter 和servlet

    通过Spring管理Filter和Servlet,不仅可以充分利用Spring的依赖注入能力,简化Filter和Servlet的配置,还能增强代码的可维护性和可扩展性。开发者无需在Filter或Servlet内部硬编码bean名称,而是通过Spring容器自动...

    Java WEB 篇九 Java servlet、filter、listener、interceptor ?.xmind

    Java WEB 篇九 Java servlet、filter、listener、interceptor 之间的区别和联系?

    Listener、Filter、Servlet与Java Web项目初始化项目例子 源代码

    通常JavaWeb项目启动时我们需要... 下面代码是模拟初始化的一个示例,可以在控制台看到程序的输出,和Listener、Filter、Servlet的启动先后顺序,强烈建议跟我一样喜欢动手的Coder操作一下,废话不多说,直接上代码了。

    Filter和Listener

    ### Filter和Listener在Java Web开发中的应用与差异 在Java Web开发中,Filter(过滤器)和Listener(监听器)是两个重要的概念,它们在Web应用程序的生命周期管理和请求处理流程中扮演着关键角色。理解它们的区别...

    Web_5_Listener和Filter1

    监听器是指Servlet API中的一个Listener接口,它允许开发人员监听Servlet的生命周期事件,例如ServletContextListener、ServletRequestListener、HttpSessionListener等。监听器可以实现一些特殊的功能,例如实现...

    特殊情况(ActionForm,Servlet, Filter, Listener)下Spring如何注入对象

    对于Servlet、Filter和Listener,由于它们通常在Web应用启动时由容器实例化,而非由Spring管理,所以也不能直接使用@Autowired注解或其他常规的注入方式。为了解决这个问题,可以使用以下策略: 1. **Servlet**: ...

    web.xml配置servlet,filter,listener加载顺序源代码

    本源码将详细介绍web.xml配置中servlet,filter,listener的加载顺序,可以让学习者更好的了解web.xml各种属性配置,自己写的东西,不足之处请大家见谅,顺便收点积分也好下资料,谢谢

    servlet+filter+lisenter 例子

    在Java Web开发中,Servlet、Filter和Listener是三个核心组件,它们构成了Web应用程序的基础架构,用于处理HTTP请求、实现业务逻辑以及管理应用的生命周期。现在,让我们深入探讨这些概念及其在实际开发中的应用。 ...

    web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序

    ### web.xml文件中配置(servlet, spring, filter, listener)的加载顺序 在Java Web应用开发中,`web.xml`文件是整个Web应用程序的核心配置文件之一,它定义了Servlet容器如何启动、初始化以及配置各个组件如...

    SpringBoot初始教程之Servlet、Filter、Listener配置详解

    我们可以使用@WebServlet、@WebFilter、@WebListener注解来配置Servlet、Filter、Listener,并使用@ServletComponentScan注解来扫描这些组件。这样,我们可以轻松地在SpringBoot中使用Servlet、Filter、Listener。

    Servlet小例子源码

    Servlet、Filter和Listener是Java Web开发中的三大核心组件,它们在构建动态Web应用程序时起着至关重要的作用。这个"Servlet小例子源码"压缩包显然提供了关于这些概念的实际应用示例,非常适合初学者来理解和掌握。 ...

Global site tag (gtag.js) - Google Analytics