JavaWeb中监听器
- ServletContext
- 生命周期监听:ServletContextListener,它有两个方法,一个在创建时调用,一个在销毁时调用;
- void contextInitialized(ServletContextEvent sce):创建Servletcontext时
- void contextDestroyed(ServletContextEvent sce):销毁Servletcontext时
- 属性监听:ServletContextAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
- void attributeAdded(ServletContextAttributeEvent event):添加属性时;
- void attributeReplaced(ServletContextAttributeEvent event):替换属性时;
- void attributeRemoved(ServletContextAttributeEvent event):移除属性时;
- HttpSession
- 生命周期监听:HttpSessionListener,它有两个方法,一个在创建时调用,一个在销毁时调用;
- void sessionCreated(HttpSessionEvent se):创建session时
- void sessionDestroyed(HttpSessionEvent se):销毁session时
- 属性监听:HttpSessioniAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
- void attributeAdded(HttpSessionBindingEvent event):添加属性时;
- void attributeReplaced(HttpSessionBindingEvent event):替换属性时
- void attributeRemoved(HttpSessionBindingEvent event):移除属性时
- ServletRequest
- 生命周期监听:ServletRequestListener,它有两个方法,一个在创建时调用,一个在销毁时调用;
- void requestInitialized(ServletRequestEvent sre):创建request时
- void requestDestroyed(ServletRequestEvent sre):销毁request时
- 属性监听:ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
- void attributeAdded(ServletRequestAttributeEvent srae):添加属性时
- void attributeReplaced(ServletRequestAttributeEvent srae):替换属性时
- void attributeRemoved(ServletRequestAttributeEvent srae):移除属性时
- 写一个监听器类:要求必须去实现某个监听器接口;
- 注册,是在web.xml中配置来完成注册!
package web.listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; /**
* ServletContext监听器
* 可以在这个监听器存放一些在Tomcat启动时就要完成的代码!
* */
@WebListener()
public class AListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("哇,我来了");
} @Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("哇,再见");
}
}
- ServletContextEvent:ServletContext getServletContext()
- HttpSessionEvent:HttpSession getSession()
- ServletRequest:
- ServletContext getServletContext();
- ServletReques getServletRequest();
- ServletContextAttributeEvent:
- ServletContext getServletContext();
- String getName():获取属性名
- Object getValue():获取属性值
- HttpSessionBindingEvent:略
- ServletRequestAttributeEvent :略
- 它用来添加到JavaBean上,而不是添加到三大域上!
- 这两个监听器都不需要在web.xml中注册!
1、HttpSessionBindingListener:当某个类实现了该接口后,可以感知本类对象添加到session中,以及感知从session中移除。
HttpSessionBindingListener:添加到javabean上,javabean就知道自己是否添加到session中了。例如让Person类实现HttpSessionBindingListener接口,那么当把Person对象添加到session中,或者把Person对象从session中移除时会调用下面两个方法:
- public void valueBound(HttpSessionBindingEvent event):当把监听器对象添加到session中会调用监听器对象的本方法;
- public void valueUnbound(HttpSessionBindingEvent event):当把监听器对象从session中移除时会调用监听器对象的本方法;
这里要注意,HttpSessionBindingListener监听器的使用与前面介绍的都不相同,当该监听器对象添加到session中,或把该监听器对象从session移除时会调用监听器中的方法。并且无需在web.xml文件中部署这个监听器。
2、取消session钝化活化功能,于tomcat\conf\context.xml文件中打开11行内容即可。
1 <Context reloadable="true">
2
3 <!-- Default set of monitored resources. If one of these changes, the -->
4 <!-- web application will be reloaded. -->
5 <WatchedResource>WEB-INF/web.xml</WatchedResource>
6 <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
7 <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
8
9 <!-- Uncomment this to disable session persistence across Tomcat restarts -->
10 <!--
11 <Manager pathname="" />
12 -->
13 </Context>
配置session活化钝化参数配置,于tomcat\conf\context.xml中添加:
<Context>
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1" >
<!-- 如果session在1分钟内没有使用,那么Tomcat就会钝化它 -->
<Store className="org.apache.catalina.session.FileStore" directory="mysession" />
<!-- 把session序列化到Tomcat\work\Catalina\localhost\listener\mysession目录下 -->
</Manager>
</Context>
3、 HttpSessionActivationListener:Tomcat会在session长时间不被使用时钝化session对象,所谓钝化session,就是把session通过序列化的方式保存到硬盘文件中。当用户再使用session时,Tomcat还会把钝化的对象再活化session,所谓活化就是把硬盘文件中的session在反序列化回内存。当session被Tomcat钝化时,session中存储的对象也被纯化,当session被活化时,也会把session中存储的对象活化。如果某个类实现了HttpSessionActiveationListener接口后,当对象随着session被钝化和活化时,下面两个方法就会被调用:
- public void sessionWillPassivate(HttpSessionEvent se):当对象感知被活化时调用本方法;
- public void sessionDidActivate(HttpSessionEvent se):当对象感知被钝化时调用本方法;
public class Person implements HttpSessionActivationListener, Serializable {
public void sessionDidActivate(HttpSessionEvent evt) {
System.out.println("session已经活化");
}
public void sessionWillPassivate(HttpSessionEvent evt) {
System.out.println("session被钝化了!");
}
}
JavaWeb中监听器的更多相关文章
- JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...
- JavaWeb中的监听器
JavaWeb中的监听器 l 事件源:三大域! ServletContext ¨ 生命周期监听:ServletContextListener,它有两个方法,一个在出生时调用,一个在死亡时 ...
- 十:JavaWeb中的监听器(一)
2.1.基本概念 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 ServletRequest等域 ...
- JavaWeb基础—监听器Listener
javaWeb三大组件: servlet listener(用的不多) filter 什么叫监听器: 初次相见:AWT 二次相见:SAX(XML解析时)Bundle 绑定 监听器是一个接口,内容由我们 ...
- Javaweb基础--->监听器listener(转发)
JavaWeb中的监听器 1.基本概念 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 Servl ...
- IT兄弟连 JavaWeb教程 监听器1
1 基本概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其实就是一个实现特定接口的普通java程序,这个程序 ...
- 深入分析JavaWeb Item38 -- 监听器(Listener)高速学习
一.监听器介绍 1.1.监听器的概念 监听器是一个专门用于对其它对象身上发生的事件或状态改变进行监听和对应处理的对象,当被监视的对象发生情况时,马上採取对应的行动.监听器事实上就是一个实现特定接口的普 ...
- 在JavaWeb中使用Log4j步骤
在JavaWeb中使用Log4J指南.每次在开始写一个项目的时候都忘记Log4J如何配置.所以写个步骤,作为记录. 第一步 下载Log4J jar包 从Apache Logging Services ...
- 在Javaweb中使用Scala
Java 是一门比较优秀的编程语言, 其最大功劳是建立非常繁荣的JVM平台生态.不过 Java 语法比较麻烦,写过 C, Python 的人总是想使用简洁的语法,又希望利用上 Java 平台的强大,因 ...
随机推荐
- Win10内部更新:警告用户别用chrome和Firefox
简评:别和 Chrome 和 Firefox 约行不,我 Edge 明明更美.屁股更翘.更性感... 微软正在测试 Windows 10 的一个更新:警告用户不要安装 Chrome 和 Firefox ...
- Java反射与自定义注解
反射,在Java常用框架中屡见不鲜.它存在于java.lang.reflact包中,就我的认识,它可以拿到类的字段和方法,及构造方法,还可以生成对象实例等.对深入的机制我暂时还不了解,本篇文章着重在使 ...
- 【性能测试】jmeter的坑(1)——如何在多网卡情况下正确连接
问题现象: 性能测试时,使用的服务器配置了双网卡,windows客户机配置了四网卡,坑爹的配置,内外网的隔离,导致在使用jmeter进行分布式测试的时候总是连接失败. 原因分析: Jmeter采用了r ...
- freemaker的函数使用
如下: <html> <body> <div class="container"> <h4>修改用户角色</h4> &l ...
- Machine learning第四周code 编程作业
1.lrCostFunction: 和第三周的那个一样的: function [J, grad] = lrCostFunction(theta, X, y, lambda) %LRCOSTFUNCTI ...
- 2018-2019 20165226 Exp7 网络欺诈防范
2018-2019 20165226 Exp7 网络欺诈防范 目录 一.实验内容说明及基础问题回答 二.实验过程 1.简单应用SET工具建立冒名网站 2.ettercap DNS spoof 3.结合 ...
- DGIS之遥感影像数据获取
1.概要 在GIS圈的同行或多或少接触过遥感,记得在大学老师就说过"数据是GIS的核心".本文介绍在国内下载遥感影像的方法. 地理空间数据云,这个是中科院计算机网络中心建设的一个免 ...
- MYSQL 缓存
在PHP.INI中query_cache_type设置为1. 即 开始MYSQL全局SQL语句 都缓存.(0 不使用) 临时关闭查询缓冲的方法:1. SELECT SQL_NO_CACHE fi ...
- MyBatis异常总结
1 Invalid bound statement 1 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...
- 使用 JFlex
参数设置和声明段 %% 词法规则段 用户代码段这个段中的所有内容将被拷贝到生成的词法类的类声明之前.在这个段中,常见的是 package 和 import 语句.我们的词法说明在这个段中引入(impo ...