Java Web监听器Listener接口原理及用法实例
监听器主要针对三个对象
ServletContext HttpSession ServletRequest使用方式
创建*Listener接口的实现类 在web.xml中注册该类在同时注册多个同接口的监听器时,执行顺序参照web.xml中的注册顺序
监听器监听类型 对象的创建和销毁 对象属性的添加、替换、移除创建实现类
// 用于监听session创建和销毁的监听器package listener;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class SessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { // 获取本次事件创建session的id String sessionId = httpSessionEvent.getSession().getId(); System.out.println('create session that id = ' + sessionId); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { // 删除session的id String sessionId = httpSessionEvent.getSession().getId(); System.out.println('session has been destroy that id = ' + sessionId); }}
在web.xml中注册
<?xml version='1.0' encoding='UTF-8'?><web-app xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://xmlns.jcp.org/xml/ns/javaee' xsi:schemaLocation='http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd' version='3.1'> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <!-- 在listener包下的SessionListener类 --> <listener-class>listener.SessionListener</listener-class> </listener></web-app>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章:
1. idea设置提示不区分大小写的方法2. IntelliJ IDEA设置默认浏览器的方法3. HTTP协议常用的请求头和响应头响应详解说明(学习)4. IntelliJ IDEA创建web项目的方法5. .NET SkiaSharp 生成二维码验证码及指定区域截取方法实现6. ASP.NET MVC通过勾选checkbox更改select的内容7. docker容器调用yum报错的解决办法8. VMware中如何安装Ubuntu9. CentOS邮件服务器搭建系列—— POP / IMAP 服务器的构建( Dovecot )10. 使用IntelliJ IDEA 配置安卓(Android)开发环境的教程详解(新手必看)
