Servlet listener

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值