HttpSessionListener的用法和实例

2021-08-02 17:53:39 浏览数 (1)

3)HttpSessionListener:

Session创建和销毁的时候可以监听到。

例 2.2.3.1

package com;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

public class MyHttpSessionListener implements

HttpSessionListener {

public void sessionCreated(javax.servlet.http.HttpSessionEvent arg0) {

System.out.println("马克-to-win session Initialized" arg0.getSession().getId());

}

public void sessionDestroyed(javax.servlet.http.HttpSessionEvent arg0) {

System.out.println("马克-to-win destroyed" arg0.getSession().getId());

}

}

web.xml加入下面这段话:

<listener>

<listener-class>com.MyHttpSessionListener</listener-class>

</listener>

用下面这个servlet测试:

package com;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class ServletHello2 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("before session create");

HttpSession hs=request.getSession();

System.out.println("after session create");

hs.invalidate();

System.out.println("after session invalidate");

}

更多请看:https://blog.csdn.net/qq_44638460/article/details/104107080

jsp

0 人点赞