`

Servlet 监听器 过滤器

阅读更多
Servlet监听器详解
http://www.21jn.net/html/85/n-285.html

Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。
接口:
目前Servlet2.4和JSP2.0总共有8个监听器接口和6个Event类,其中HttpSessionAttributeListener与
HttpSessionBindingListener皆使用HttpSessionBindingEvent;HttpSessionListener和HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener对应的Event如下所示:

Listener接口
Event类

ServletContextListener
ServletContextEvent

ServletContextAttributeListener
ServletContextAttributeEvent

HttpSessionListener
HttpSessionEvent

HttpSessionActivationListener

HttpSessionAttributeListener
HttpSessionBindingEvent

HttpSessionBindingListener

ServletRequestListener
ServletRequestEvent

ServletRequestAttributeListener
ServletRequestAttributeEvent





一 ServletContext相关监听接口

补充知识:通过ServletContext 的实例可以存取应用程序的全局对象以及初始化阶段的变量。
在JSP文件中,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。

注意:全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由<context-param>元素所设定的变量,它的范围也是Application范围,例如:

<context-param>
  <param-name>Name</param-name>
  <param-value>browser</param-value>
</context-param>

当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:
  String name = (String)application.getInitParameter("Name");
或者使用EL时:
  ${initPara.name}
若是在Servlet中,取得Name的值方法:
  String name = (String)ServletContext.getInitParameter("Name");

1.ServletContextListener:
用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。
ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。

ServletContextListener接口的方法:
  void contextInitialized(ServletContextEvent sce) 
通知正在接受的对象,应用程序已经被加载及初始化。
  void contextDestroyed(ServletContextEvent sce) 
通知正在接受的对象,应用程序已经被载出。
ServletContextEvent中的方法:
ServletContext getServletContext()
取得ServletContext对象

2.ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。

ServletContextAttributeListener接口方法:
void attributeAdded(ServletContextAttributeEvent scab)
若有对象加入Application的范围,通知正在收听的对象
void attributeRemoved(ServletContextAttributeEvent scab)
若有对象从Application的范围移除,通知正在收听的对象
void attributeReplaced(ServletContextAttributeEvent scab)
若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象


ServletContextAttributeEvent中的方法:
java.lang.String getName()
回传属性的名称
java.lang.Object getValue()
回传属性的值

二、HttpSession相关监听接口
1.HttpSessionBindingListener接口
注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener

当我们的类实现了HttpSessionBindingListener接口后,只要对象加入Session范围(即调用HttpSession对象的setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的removeAttribute方法的时候或Session Time out的时候)时,容器分别会自动调用下列两个方法:
void valueBound(HttpSessionBindingEvent event)
void valueUnbound(HttpSessionBindingEvent event)

思考:如何实现记录网站的客户登录日志, 统计在线人数?

2.HttpSessionAttributeListener接口
HttpSessionAttributeListener监听HttpSession中的属性的操作。
当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。这和ServletContextAttributeListener比较类似。

3.HttpSessionListener接口
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。

4.HttpSessionActivationListener接口
  主要用于同一个Session转移至不同的JVM的情形。

四、ServletRequest监听接口
1.ServletRequestListener接口和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
2.ServletRequestAttributeListener接口和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest

Servlet监听器概念特点常用概述
http://developer.51cto.com/art/200907/134146.htm

Servlet监听器用于监听一些重要事件的发生,Servlet监听器对象可以在事情发生前、发生后可以做一些必要的处理。
Servlet监听器监听器概述

◆Listener是Servlet的监听器

◆可以监听客户端的请求、服务端的操作等。

◆通过监听器,可以自动激发一些操作,如监听在线用户数量,当增加一个HttpSession时,给在线人数加1。

◆编写监听器需要实现相应的接口

◆编写完成后在web.xml文件中配置一下,就可以起作用了

◆可以在不修改现有系统基础上,增加web应用程序生命周期事件的跟踪

常用的Servlet监听器监听接口

◆ServletContextAttributeListener

监听对ServletContext属性的操作,比如增加/删除/修改

◆ServletContextListener

监听ServletContext,当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。

◆HttpSessionListener

监听HttpSession的操作。当创建一个Session时,激发session Created(SessionEvent se)方法;当销毁一个Session
时,激发sessionDestroyed (HttpSessionEvent se)方法。

◆HttpSessionAttributeListener

监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

使用范例:

计算在线用户数量的Linstener

(1)Package xxx;

public class OnlineCounter
{      
private static long online = 0; 
public static long getOnline()
{       
return online;    
}     
public static void raise()
{        
online++;     
}     
public static void reduce()
{         
online--;


import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;  
public class OnlineCounterListener implements HttpSessionListener{     
public void sessionCreated(HttpSessionEvent hse)
{          
OnlineCounter.raise();       
}     
public void sessionDestroyed(HttpSessionEvent hse)
{           
OnlineCounter.reduce();     
}   

在需要显示在线人数的JSP中可是使用目前在线人数:

﹤%@ page import=“xxx.OnlineCounter" %﹥ 
﹤%=OnlineCounter.getOnline()%﹥
退出会话(可以给用户提供一个注销按钮):

﹤form action="exit.jsp" method=post﹥    
﹤input type=submit value="exit"﹥  
﹤/form﹥  exit.jsp: ﹤%session.invalidate() ;%﹥

在web.xml中加入:

﹤listener﹥
﹤listener-class﹥
servletlistener111111.SecondListener
﹤/listener-class﹥
﹤/listener﹥

使用servlet过滤器和监听器
http://charmo.iteye.com/blog/170259

1. Servlet过滤器基础
Servlet过滤器是Servlet的一种特殊用法,主要用来完成一些通用的操作。比如编码的过滤,判断用户的登陆状态等等。Servlet过滤器的适用场合:
A.认证过滤
B.登录和审核过滤
C.图像转换过滤
D.数据压缩过滤
E.加密过滤
F.令牌过滤
G.资源访问触发事件过滤
Servlet过滤器接口的构成:
所有的Servlet过滤器类都必须实现javax.servlet.Filter接口。这个接口含有3个过滤器类必须实现的方法:
方法 说明
init(FilterConfig cfg) 这是Servlet过滤器的初始化方法,性质等同与servlet的init方法。
doFilter(ServletRequest,ServletResponse,FilterChain) 完成实际的过滤操作,当请求访问过滤器关联的URL时,Servlet容器将先调用过滤器的doFilter方法。FilterChain参数用于访问后续过滤器
destroy() Servlet容器在销毁过滤器实例前调用该方法,这个方法中可以释放Servlet过滤器占用的资源。,性质等同与servlet的destory()方法。
Servlet过滤器的创建步骤:
A.实现javax.servlet.Filter接口的servlet类
B.实现init方法,读取过滤器的初始化函数
C.实现doFilter方法,完成对请求或过滤的响应
D.调用FilterChain接口对象的doFilter方法,向后续的过滤器传递请求或响应
F.在web.xml中配置Filter
2.使用过滤器处理中文问题
   当用用户登陆页面输入帐号时,如果输入是中文,后台servlet再次输出这个内容时,可能就会是乱码,这是因为serlvet中默认是以ISO-8859-1格式编码的,如果后台有多个Servlet,多个参数,这样就不合适,这个问题,我们可以通过一个过滤器统一解决,使后台的输出输出都支持中文!将ISO-8859-1转码为GBK的那段代码!
3.使用过滤器认证用户:
每个过滤器也可以配置初始化参数,可以将不需要过滤的地址配置到这个Filter的配置参数中,过滤时,如果请求地址在配置参数中,则放行,这样就避免了在程序中硬编码。每个Filter中初始化时,都可以得到配置对象,在Filter中配置二个不需要过滤的地址,一个是登陆页面,一个是执行登陆认证的servlet;
4.Servlet监听器
类似与Swing界面应用开发,Servlet也可以创建监听器,以对Servlet容器,或Servlet中以象的事件做出反应。Servlet监听器主要有以下几种:
ServletRequestListener ,ServletRequestAttributeListener,
HttpSessionActivationListener ,HttpSessionBindingListener ,
HttpSessionAttributeListener,HttpSessionListener,
ServletContextListener等等。
这些监听器主要用来监听session,request,application这三个对象里存取数据的变化。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics