18,ServletContext事件,监听器
ServletContext相关监听器,ServletContextListener与ServletContextAttributeListener
(1)ServletContextListener
ServletContextListener是"生命周期监听器",如果想要知道何时Web应用程序已经初始化或即将结束销毁,可以实现ServletContextListener:
package javax.servlet;
import java.util.EventListener;
public interface ServletContextListener extends EventListener{
public void contextInitialized(ServletContextEvent sce);
public void contextDestoryed(ServletContextEvent sce);
}
在Web应用程序初始化后或即将结束销毁前,会调用ServletContextListener实现类对应的contextInitialized或contextDestoryed。可以在contextInitialized()中实现应用
程序资源的准备动作,在contextDestroyed()实现释放应用程序资源的动作。
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener //使用WebListener标注
public class ContextParameterReader implements ServletContextListener{//实现ServletContextListener
public void contextInitialized(ServletContextEvent sce){
ServletContext context=sce.getServletContext();//取得ServletContext
String avatars=context.getInitParameter("AVATAR");//取得初始参数
context.setAttribute("avatars", avatars);//设置ServletContext属性
}
public void contextDestroyed(ServletContextEvent sce){
}
}
ServletContextListener可以直接使用@WebListener标注,而且必须实现ServletContextListener接口,这样容器会在启动时加载并运行对应的方法。当
Web容器调用contextInitialized()或contextDestoryed()时,会传入ServletContext-Event,其封装了ServletContext,可以通过ServletContextEvent的getServletContext()方法取得
ServletContext,通过ServletContext的getInitParameter()方法来读取初始参数,因此Web应用程序初始参数常被成为ServletContext初始参数。
在整个Web应用程序生命周期,Servlet需共享的资料可以设置为ServletContext属性。由于ServletContext在Web应用程序存活期间会一直存在,所以设置为ServletContext属性
的数据,除非主动移除,否则也是一直存活于Web应用程序中。
可以通过ServletContext的setAttribute()方法设置对象为ServletCOntext属性,之后可通过ServletContext的getAttribute()方法取出该属性。若要移除属性,则通过ServletContext的removeAttribute()方法。
因为@WebListener没有设置初始参数的属性,所以仅适用于无须设置初始参数的情况。如果需要设置初始参数,可以在web.xml中设置:
<context-param>
<param-name>AVATAR</param-name>
<param-value>/avatar</param-value>
</context-param>
(2)ServletContextAttributeListener
ServletContextAttributeLIstener是"监听属性改变的监听器".
import java.util.EventListener;
public interface ServletContextAttributeListener extends EventListener{
public void attributeAdded(ServletContextAttributeEvent scab);
public void attributeRemoved(ServletContextAttributeEvent scab);
public void attributeReplaced(ServletContextAttributeEvent scab);
}
当在ServletContext中添加属性,移除属性或替换属性时,相对应的attributeAdded(),attributeRemoved()与attributeReplaced()方法就会被调用。
19,HttpSession事件,监听器
与HttpSession相关的监听器有四个:HttpSessionListener,HttpSessionListener,HttpSessionBindingListener与HttpSessionActivationListener。
(1)HttpSessionListener
其为生命周期监听器,如果想要在HttpSession对象创建或结束时,做些相对应动作,可以实现HttpSessionListener。
import java.util.EventListener;
public interface HttpSessionListener extends EventListener{
public void sessionCreated(HttpSessionEvent se);
public void sessionDestroyed(HttpSessionEvent se);
}
在HttpSession对象初始化或结束前,会分别调用sessionCreated()与sessionDestroyed()方法,可以通过传入的HttpSessionEvent,使用getSession()取得HttpSession,
以针对会话对象作出相对应的创建或结束处理操作。
(2)HttpSessionAttributeListener
HttpSessionAttributeListener是属性改变监听器,当在会话对象中加入属性,移除属性或替换属性时,相对应的attributeAdded(),attributeRomved()与attributeReplaced()
方法就会被调用,并分别传入HttpSessionBIndingEvent。
import java.util.EventListener;
public interface HttpSessionAttributeListener extends EventListener{
public void attributeAdded(HttpSessionBIndingEvent se);
public void attributeRemoved(HttpSessionBIndingEvent se);
public void attributeReplaced(HttpSessionBIndingEvent se);
}
HttpSessionBIndingEvent有个getName()方法,可以取得属性设置或移除时指定的名称,而getValue()可以取得属性设置或移除时的对象。
(3)HttpSessionBIndingListener
HttpSessionBindingListener是对象绑定监听器,如果有个即将加入HttpSession的属性对象,希望在设置给HttpSession成为属性或从HttpSession成为属性或从
HttpSession中移除时,可以收到HttpSession的通知,则可以让该对象实现HttpSessionBIndingListener接口。
import java.util.EventListener;
public interface HttpSessionBIndingListener extends EventListener{
public void valueBound(HtpSessionBIndingEvent event);
public void valueUnbound(HttpSessionBIndingEvent event);
}
这个接口是实现加入HttpSession的属性对象,不需注释或在web.xml中设置。当实现此接口的属性对象被加入HttpSession或从中移除时,就会调用对应的valueBound()与valueUnbound()方法,并传入HttpSessionBIndingEvent对象,可以通过该对象的getSession取得Httpsession对象。
(4)HttpSessionActivationListener
HttpSessionActivationLIstener是对象迁移监听器,其定义两个方法sessionWillPassivate()与sessionDidActivate().一般情况下几乎不会使用到HttpSessionActivationLIstener
。在使用分布式环境时,应用程序的对象可能分散在多个JVM中。当HttpSession要从一个JVM迁移至另一个JVM时,必须先在原本的JVM序列化所有的属性对象,
在这之前若属性对象有实现HttpSessionActivationListener,就会调用sessionWillPassivate()方法,而HttpSession迁移至另一个JVM后,就会对所有属性对象作反序列化,
此时会调用sessionDidctivate()方法。
20,HttpServletRequest事件,监听器
与请求相关的监听器有三个:ServletRequestListener,ServletRequestAttributeListener与AsyncListener。
(1)ServletRequestListener
ServletRequestListener是生命周期监听器,如果想要在HttpServletRequest对象生成或结束时做些相对应的操作,则可以实现ServletREquestListener。
import java.util.EventListener;
public interface ServletRequestListener extends EventListener{
public void requestDestroyed(ServletRequestEvent sre);
public void requestInitialized(ServletRequestEvent sre);
}
在ServletRequest对象初始化或结束前,会调用requestINitialized()与requestDestroyed()方法,可以通过传入的ServletRequestEvent来取得ServletReuqest,以针对请求对象
做出相对应的初始化或结束处理动作。
(2)ServletRequestAttributeListener
ServletRequestAttributeListener是属性改变监听器,在请求对象中加入属性,移除属性或替换属性时,相对应的attributeAdded(),attributeRemoved()与attributeReplaced()方法
就会被调用,并分别传入ServletRequestAttributeEvent。
ServletRequestAttributeListener有个getName()方法,可以取得属性设置或移除时指定的名称,而getValue()则可以取得属性设置或移除时的对象。