开发一个签到系统,需要记录登录、退出的时间等信息, 如果用户不点击退出直接关闭浏览器, 想使用HttpSessionListener监听sessionDestroyed来记录用户退出时间 .配置了监听器, 没起作用, 求大神指点. 当前web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <listener> <listener-class>listener.MySessionListener</listener-class> </listener> <filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>config.SystemConfig</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
MySessionListener.kt class MySessionListener:HttpSessionListener { override fun sessionDestroyed(p0: HttpSessionEvent?) { val session: HttpSession? = p0?.session val user: Staff? = session?.getAttribute("user") as Staff? if (user!=null){ val ip: String? = session?.getAttribute("ip") as String? var loginRecord: LoginRecord = LoginRecord() loginRecord.staffId = user.id loginRecord.did = "out" loginRecord.ip = ip loginRecord.save() } println("session was destroyed") } override fun sessionCreated(p0: HttpSessionEvent?) { println("session was create") } }