概述:
Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。
接口:
目前Servlet2.4和JSP2.0总共有8个监听器接口和6个Event类,其中
HttpSessionAttributeList<wbr><span style="font-size:13px">ener与HttpSessionBindingListen</span><wbr><span style="font-size:13px">er皆使用HttpSessionBindingEvent;</span></wbr></wbr>
HttpSessionListener和HttpSessionActivationLis<wbr><span style="font-size:13px">tener则都使用HttpSessionEvent;</span></wbr>
其余Listener对应的Event如下所示:
<wbr></wbr>
|
Listener接口 |
Event类 |
|
ServletContextListener |
ServletContextEvent |
|
ServletContextAttributeL<wbr>istener</wbr> |
ServletContextAttributeE<wbr>vent</wbr> |
|
HttpSessionListener |
HttpSessionEvent |
|
HttpSessionActivationLis<wbr>tener</wbr> | |
|
HttpSessionAttributeList<wbr>ener</wbr> |
HttpSessionBindingEvent |
|
HttpSessionBindingListen<wbr>er</wbr> | |
|
ServletRequestListener |
ServletRequestEvent |
|
ServletRequestAttributeL<wbr>istener</wbr> |
ServletRequestAttributeE<wbr>vent</wbr> |
<wbr></wbr>
分别介绍:
一 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.ServletContextAttributeL<wbr><span style="font-size:13px"><strong>istener:</strong>用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeL</span><wbr><span style="font-size:13px">istener接口。</span></wbr></wbr>
ServletContextAttributeL<wbr><span style="font-size:13px">istener接口方法:<br><strong>void attributeAdded(ServletContextAttributeE</strong></span><wbr><span style="font-size:13px"><strong>vent scab)</strong><br> 若有对象加入Application的范围,通知正在收听的对象<br><strong>void attributeRemoved(ServletContextAttributeE</strong></span><wbr><span style="font-size:13px"><strong>vent scab)</strong><br> 若有对象从Application的范围移除,通知正在收听的对象<br><strong>void attributeReplaced(ServletContextAttributeE</strong></span><wbr><span style="font-size:13px"><strong>vent scab)</strong><br> 若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象</span></wbr></wbr></wbr></wbr>
ServletContextAttributeE<wbr><span style="font-size:13px">vent中的方法:<br><strong>java.lang.String getName()</strong><br>
回传属性的名称<br><strong>java.lang.Object getValue()</strong><br>
回传属性的值</span></wbr>
二、HttpSession相关监听接口
1.HttpSessionBindingListen<wbr><span style="font-size:13px"><strong>er接口</strong></span></wbr>
注意:HttpSessionBindingListen<wbr><span style="font-size:13px">er接口是唯一不需要再web.xml中设定的Listener</span></wbr>
当我们的类实现了HttpSessionBindingListen<wbr><span style="font-size:13px">er接口后,只要对象加入Session范围(即调用HttpSession对象的setAttribute方法的时候)或从Session范围中移出</span></wbr>
(即调用HttpSession对象的removeAttribute方法的时候或SessionTime out的时候)时,容器分别会自动调用下列两个方法:
void valueBound(HttpSessionBindingEvent event)
void valueUnbound(HttpSessionBindingEvent event)
思考:如何实现记录网站的客户登录日志, 统计在线人数?
2.HttpSessionAttributeList<wbr><span style="font-size:13px"><strong>ener接口</strong></span></wbr>
HttpSessionAttributeList<wbr><span style="font-size:13px">ener监听HttpSession中的属性的操作。</span></wbr>
当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se)方法;
当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEventse)方法;
当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEventse) 方法。这和ServletContextAttributeL<wbr><span style="font-size:13px">istener比较类似。</span></wbr>
3.HttpSessionListener接口
HttpSessionListener监听HttpSession的操作。
当创建一个Session时,激发sessionCreated(HttpSessionEvent se)方法;
当销毁一个Session时,激发sessionDestroyed(HttpSessionEvent se)方法。
4.HttpSessionActivationLis<wbr><span style="font-size:13px"><strong>tener接口</strong><br> 主要用于同一个Session转移至不同的JVM的情形。</span></wbr>
四、ServletRequest监听接口
1.ServletRequestListener接口和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
2.ServletRequestAttributeL<wbr><span style="font-size:13px"><strong>istener</strong>接口和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest</span></wbr>
1496

被折叠的 条评论
为什么被折叠?



