从作用域范围来说,Servlet的作用域有ServletContext,HttpSession,ServletRequest.
Context范围:
ServletContextListener:
对一个应用进行全局监听.随应用启动而启?随应用消p消׃要有两个Ҏ:
contextDestroyed(ServletContextEvent event)
在应用关闭的时候调?BR>contextInitialized(ServletContextEvent event)
在应用启动的时候调?BR>
q个监听器主要用于一些随着应用启动而要完成的工?也就是很多h说的我想在容?BR>启动的时候干..........
一般来说对"全局变量"初始??BR>public void contextInitialized(ServletContextEvent event){
ServletContex sc = event.getServletContext();
sc.setAttribute(name,value);
}
以后你就可以在Q何servlet中getServletContext().getAttribute(name);
我最喜欢用它来做守护性工?是在contextInitialized(ServletContextEvent event)
Ҏ中实C个Timer,然后p应用在每ơ启动的时候让q个Timer工作:
public void contextInitialized(ServletContextEvent event){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
//do any things
}
},0,旉间隔);
}
有h说Timer只能规定从现在开始的多长旉?每隔多久做一ơ事或在什么时间做
一ơ事,那我惛_每月1h每天12点做一工作如何做?
你只要设一个间?然后每次判断一下当时是不是那个旉D就行了?比如每月一号做,那你
旉间隔设ؓ??4时一个@?然后在runҎ中判断当时日期new Date().getDate()==1
p了啊.如果是每天的12?那你旉间隔设ؓ时,然后在run中判断new Date().getHour()
==12,再做某事p?
ServletContextAttributeListener:
q个监听器主要监听ServletContex对象在setAttribute()和removeAttribute()的事?注意
也就是一?全局变量"在被Add(W一ơset),replace(对已有的变量重新赋?和remove的时?
分别调用下面三个Ҏ:
public void attributeAdded(ServletContextAttributeEvent scab)q个Ҏ不仅可以知道
哪些全局变量被加q来,而且可获取容器在启动时自动设|了哪些context变量:
public void attributeAdded(ServletContextAttributeEvent scab){
System.out.println(scab.getName());
}
public void attributeRemoved(ServletContextAttributeEvent scab)
public void attributeReplaced(ServletContextAttributeEvent scab)
Session范围:
HttpSessionListener:
q个监听器主要监听一个Session对象被生成和销毁时发生的事?对应有两个方?
public void sessionCreated(HttpSessionEvent se)
public void sessionDestroyed(HttpSessionEvent se)
一般来?一个session对象被create?可以说明有一个新客端q入.可以用来_略l计在线?BR>?注意q不是精的,因ؓq个客户端可能立卛_关闭?但sessionDestroyedҎ却会按一?BR>的策略很久以后才会发?
HttpSessionAttributeListener:
和ServletContextAttributeListener一?它监听一个session对象的Attribut被Add(一个特?BR>名称的Attribute每一ơ被讄),replace(已有名称的Attribute的D重设)和remove时的事g.
对就的有三个Ҏ.
public void attributeAdded(HttpSessionBindingEvent se)
public void attributeRemoved(HttpSessionBindingEvent se)
public void attributeReplaced(HttpSessionBindingEvent se)
上面的几个监听器的方?都是在监听应用逻辑中servlet逻辑中发生了什么事,一般的来说.
我们只要完成逻辑功能,比如session.setAttribute("aaa","111");我只要把一个名为aaa的变?BR>攑֜session中以便以后我能获取它,我ƈ不关心当session.setAttribute("aaa","111");发生?BR>我还要干什?(当然有些时候要利用?,但对于下面这个监听器,你应该好好发解一?
HttpSessionBindingListener:
上面的监听器都是作ؓ一个独立的Listener在容器中控制事g?而HttpSessionBindingListener
对在一对象中监听该对象的状?实现了该接口的对象如果被作ؓvalue被addC个session中或?BR>session中remove,它就会知道自己已l作Z个session对象或已l从session删除,q对于一些非
UJAVA对象,生命周期长于session的对?以及其它需要释放资源或改变状态的对象非常重要.
比如:
session.setAttribute("abcd","1111");
以后session.removeAttribute("abcd");因ؓabcd是一个字W中,你从session中remove?它就?BR>自动被垃圑֛收器回收,而如果是一个connection:(只是举例,你千万不要加connection往session
中加?
session.setAttribute("abcd",conn);
以后session.removeAttribute("abcd");q时q个conn被从session中remove?你已l无法获取它
的句?所以你Ҏ没法关闭?而在没有remove之前你根本不知道什么时候要被remove,你又无法
close(),那么q个connection对象死?另外q有一些对象可以在被加入一个session时要锁定
q要被remove时要解锁,应因你在E序中无法判断什么时候被remove(),addq好操作,我可以先加锁
再add,但remove后你就找不到它的句柄了,Ҏ没法解锁,所以这些操作只能在对象自n中实?
也就是在对象被add时或remove旉知对象自己回调相应的方?
MyConn extends Connection implements HttpSessionBindingListener{
public void valueBound(HttpSessionBindingEvent se){
this.initXXX();
}
public void valueUnbound(HttpSessionBindingEvent se){
this.close();
}
}
session.setAttribute("aaa",new MyConn());
q时如果调用session.removeAttribute("aaa"),则触发valueUnboundҎ,׃自动关闭自己.
而其它的需要改变状态的对象了是一?
另外q有一个HttpSessionActivationListener监听器是实现分布式应用中session同步?不作
多介l?如果有要实现该功能的朋友可以和我联系.
在servlet2.4?对于request范围已经实现对应的监听器:
ServletRequestListenerQServletRequestAttributeListener