监听器

31 篇文章 0 订阅
24 篇文章 0 订阅


事件监听机制:
事件源
事件
监听器
事件处理


Web监听器:


实现了SUN公司规定的一个特定接口XxxListener,以监听域对象变化情况.
Listeners:
ServletContextListener,HttpSessionListener----->两个常用,
ServletRequestListener--->.
HttpSessionBindingListener----->用于JavaBean本身


ServletContextListener
methods:
contextInitialized();
contextDestroyed();

ServletContextAttributeListener

事件源:application/ServletContext/域对象
事件:setAttribute/removeAttributr,
setAttribute有二种含义:增加,更新
事件监听器:web服务器:Tomcat
事件处理程序:
methods:
attributeAdded();
attributeRemoved();
attributeReplaced();

SrvletRequestListener
methods:
requestInitialized();
requestDestroyed();
ServletRequestAttributeListener

事件源:request/域对象
事件:setAttribute/removeAttributr,
setAttribute有二种含义:增加,更新
事件监听器:web服务器---->Tomcat
事件处理程序:
methods:
attributeAdded();
attributeRemoved();
attributeReplaced();


HttpSessionListener
methods:
sessionCreated();
sessionDestroyed();

访问jsp页面,在默认情况下,Web服务器自动产生HttpSession对象,存于服务器,
关闭浏览器,原HttpSession不能立即销毁,
默认有30分钟的生命周期,Web服务器删除HttpSession的时间不精确.


HttpSessionAttributeListener
事件源:httpSession/域对象
事件:setAttribute/removeAttributr,
setAttribute("javabean",javabean变量);
setAttribute("常量",常量);

事件监听器:web服务器--->Tomcat
事件处理程序:
methods:
attributeAdded();
attributeRemoved();
attributeReplaced();






HttpSessionBindingListener

JavaBean实现这个接口
使这个JavaBean变成事件源
JavaBean监听自己

Web中的监听器,只监听三个域对象,因此这个监听可不用在xml中配置监听注册

methods:
valueBound();
valueUnbound();

//激活/钝化,由web服务器完成。
HttpSessionActivationListener

sessionDidActivate();
使用这个,需要在JavaBean中实现java.io.Serializable,序列化
sessionWillPassivate();

配置context.xml文件:
在web服务器(tomcat)中context.xml配置的话,是全局配置
在当前项目中的META-INF中配置一个context.xml文件,为局变配置
方法:
<Context>
<Manger className="类路径" maxIdleSwap="时间">



</Manager>

</Context>
存储路径:
..\apache-tomcat-6.0.29\work\Catalina\localhost
在jsp页面中声明
<%@ page session="true"%>

<%

session.invalidate();
%>
在XML中设置
<session-config>
  <session-timeout>1*60*1000</session-timeout>
</session-config>

Session扫描器(单例)

用集合来封装所有的session
List<HttpSession> sessionListener=new ArrayList<HttpSession>{};


销毁HttpSession的几种方式:
1,web.xml配置session的有效时间
2,session.invalidate();
3,重新部署和服务器重启





在XML中配置:
<!-- 监听器 -->
先配置的先执行.
  <listener>
  <display-name>ServletListener</display-name>
  <listener-class>listener.servlet.ServletListener</listener-class>
  </listener>




 
定时器 
java.util.Timer;计时器.
method:
schedule(参数...);




JavaBean的四种状态;
绑定:
当JavaBean绑定到HttpSession中
移除:
从HttpSession中移除JavaBean
事件源:JavaBean对象
事件: HttpSession.setAttribute()和HttpSession.removerAttribute();只针对HttpSession
事件监听类:JavaBean对象
事件监听器:HttpSessionBindingListener



当一个JavaBean绑定到HttpSession中,HttpSessionAttributeListener和HttpSessionBindingListener 都会被监听到.
如果只是一个普通变量,那么只能用HttpSessionAttributeListener监听




钝化:
将在内存中的HttpSession,转移到外存的过程


激活:
将外存中的HttpSession,转移到内存中的过程.
 


 
































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wscwsc58888

只为正式环境创作

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值