Java Web监听器Listener接口原理及用法实例

网友投稿 265 2023-04-07

Java Web监听器Listener接口原理及用法实例

监听器主要针对三个对象

ServletContext

HttpSession

ServletRequest

使用方式

创建*Listener接口的实现类http://

在web.xml中注册该类

http://

在同时注册多个同接口的监aCHLOYqgjb听器时,执行顺序参照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中注册

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"

id="WebApp_ID" version="3.1">

Archetype Created Web Application

index.jsp

listener.SessionListener

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"

id="WebApp_ID" version="3.1">

Archetype Created Web Application

index.jsp

listener.SessionListener

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何快速搭建一个自己的服务器的详细教程(java环境)
下一篇:eclipse实现可认证的DH密钥交换协议
相关文章

 发表评论

暂时没有评论,来抢沙发吧~