现在的位置: 首页 > 综合 > 正文

Servlet的监听器

2018年05月09日 ⁄ 综合 ⁄ 共 2148字 ⁄ 字号 评论关闭

1. Servlet监听器介绍
Web应用程序的事件监听器是指实现了一个或多个Servlet事件监听接口的类。当Web应用部署时,它们被Web实例化和注册。一个监听器只能监听在同一个WAR中的事件。为了能够接收事件通知,实现监听器接口的类必须在Web.xml中配置

2. Servlet监听器的接口
1) ServletContextAttributeListener 用于监听ServletContext的属性改变,方法如下:
void attributeAdded(ServletContextAttributeEvent scab) 通知一个属性被添加到Servlet的上下文中。
void attributeRemoved(ServletContextAttributeEvent scab) 通知一个已经存在的属性从Servlet上下文中删除了。
void attributeReplaced(ServletContextAttributeEvent scab) 通知一个属性已经从Servlet上下文中被替换掉了。

2) ServletContextListener用于监听ServletContext的创建和销毁,方法如下:
void contextDestroyed(ServletContextEvent sce) 通知Servlet上下文即将关闭。
void contextInitialized(ServletContextEvent sce) 通知Web应用程序正在开启初始化进程。

3) ServletRequestAttributeListener用于监听Servlet请求中的属性改变,方法如下:
void attributeAdded(ServletRequestAttributeEvent srae) 通知一个新的属性被添加到Servlet请求中。
void attributeRemoved(ServletRequestAttributeEvent srae) 通知一个已经存在的属性从请求中删除了。
void attributeReplaced(ServletRequestAttributeEvent srae) 通知一个Servlet上下文中的属性被替换掉了。

4) ServletRequestListener用于监听Servlet中的请求对象创建和销毁,方法如下:
void requestDestroyed(ServletRequestEvent sre) 通知请求将要从Web应用程序中销毁了。
void requestInitialized(ServletRequestEvent sre) 通知请求将要在Web应用程序中舒适化了。

5) HttpSessionActionListener用于监听HTTP会话的激活和钝化,方法如下:
Object getAttribute(String name) 从Session中返回一个指定名字的对象,如果没有该对象存在,则返回空。
Enumeration getAttributeNames() 从Session中返回所有邦定在Session中的属性名字的一个枚举。
long getCreationTime() 当Session创建的时候,返回一个自1970年1月1号以后的,以毫秒为单位的时间。
String getId() 返回一个标识这个Session唯一性的字符串。
long getLastAccessedTime() 返回客户端与这个会话联合的最后发送请求的时间,这个时间是自1970年1月1号以后的以毫秒为单位。
int getMaxInactiveInterval() 返回最大的时间间隔,这个时间表示Servlet在两次访问的间隔。
ServletContext getServletContext() 返回属于这个Session的ServletContext。
void invalidate() 使Session与邦定对象无效。
boolean isNew() 如果客户端还不知道这个Session,或者客户端还没有加入到这个Session中,则返回true
void removeAttribute(String name)
          Removes the object bound with the specified name from this session.
void setAttribute(String name, Object value)
          Binds an object to this session, using the name specified.
void setMaxInactiveInterval(int interval)
          Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

6) HttpSessionAttributeListener用于监听HTTP会话中属性的改变,方法如下:

7) HttpSessionBindingListener用于监听HTTP会话中属性的增加和删除,方法如下:

8) HttpSessionListener用于监听HTTP会话中的创建和销毁,方法如下:

抱歉!评论已关闭.