Use the listener to track the lifecycle of the Session and the properties of the session

xiaoxiao2021-04-10  488

1. Use the listener to track the life cycle of the session

Method: Create a listener that implements the HTTPSessionListener interface, then register the class again at the deployment description file.

Note: Servlet API provides a Javax.Servlet.http.httpSessionListener interface to respond to sessions creation and destruction. This interface defines two methods: sessionCreated () and sessionDestoryed (), each method accepts an HttpSessionEvent parameter. (Implementing a class to provide a parameter-free constructor).

E G: Public Class SessionListener Implements HttpSessionListenceListence {

PUBLIC sessionListener () {};

Public void sessioncreated (httpsessioneverive) {

// code

}

Public void sessionDestoryed (httpsessionever event event) {

// code

}

}

Finally, register the listener in web.xml

com.company.listener.SessionListener>

II. Listener tracking the properties of the session

Method: Create a listener that implements the HTTPSESESSATRIBURTELISTENER interface and register the class again at the deployment description file.

Note: Servlet API provides a javax.servlet.http.httpsessionattriburtelistener interface that defines three ways: Attributeadd (), AttributeRemove (), AttributeREPLACE (), three methods accept an HttpSessionBingDingevent type parameter. (Implementing a class to provide a parameter-free constructor).

E g: public class sessionattributeListener imports httpsessionattributeListenter {

Public sessionattributeListener () {};

Public void attributeadded (httpsessionBindingEvent evenet) {

// code

}

Public void attributemove (httpsessionBindingEvent evenet) {

// code

}

Public void attributeplace (httpsessionBindingEvent evenet) {

}

Finally, register the listener in web.xml

com.company.listener.SessionAttributeListener>

转载请注明原文地址:https://www.9cbs.com/read-133226.html

New Post(0)