国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Java Web監聽器Listener接口原理及用法實例

瀏覽:2日期:2022-08-30 14:09:01

監聽器主要針對三個對象

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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 聂荣县| 沂源县| 怀化市| 雷波县| 怀宁县| 寿阳县| 广东省| 宜川县| 阜宁县| 龙岩市| 巨鹿县| 博爱县| 平南县| 芜湖县| 新余市| 新野县| 黄山市| 庄浪县| 清镇市| 澎湖县| 富民县| 安陆市| 通辽市| 舟曲县| 旺苍县| 临武县| 榆林市| 和平区| 彩票| 尼玛县| 布拖县| 定结县| 盈江县| 苏尼特右旗| 蓬莱市| 贺州市| 北宁市| 兴宁市| 乌苏市| 墨玉县| 新泰市|