Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the principle and function of Litener listener?

Shulou Source: shulou.com Published: 2022-06-02 09:51:23 09月11日 Update

This article mainly explains "what is the principle and function of Litener listener". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "what is the principle and function of Litener listener" together!

Several listeners are defined in the Servlet API, most, though not all, will listen for some form of session activity, subscribe to an event by implementing the listener interface for the event, and then add configuration to the deployment descriptor, or add comments to the class

@javax.servlet.annotation.WebListener (but not required for colleagues to use either)

If desired, you can implement multiple listener interfaces in a single class, although you don't want to add code for different businesses to the same class. When an event occurs, it triggers the event publication, and the container calls the method in the corresponding event listener.

Listener

Listen to three domain objects

ServletContext

ServletRequest

HttpSession

Listen for the creation and destruction of three objects

ServletContextListener

Trigger when servlet starts and destroys

ServletRequestListener

Each request will trigger, refresh the next page will trigger

HttpSessionListener

A browser only creates one session

Create: The first call to request.getSession in java

Destruction:

session timeout

Manually destroy session

Server shutdown abnormal

Listen for attribute changes of three objects (add, replace, delete)

ServletContextAttributeListener

application.setAttribute("aa","fffffff 66666 aaa aaaa");

System.out.println("add"+event.getName()+event.getValue());

application.setAttribute("aa","f33333 aaa aaaa");

application.removeAttribute("aa");

ServletRequestAttributeListener

HttpSessionAttributeListener

Listen to the status of javabean in session

These two interfaces need javabean implementation, which is to make javabean aware of its own state.

HttpSessionActivationListener (passivation (session write to disk) and activation (disk write session))

passivation: javabean persistent from session to disk

Activation: javabean writes from disk to session

Also write to the specified class to implement 2 interfaces implements HttpSessionActivationListener,Serializable{

javabean passivation activation times can be modified via configuration files

/META-INFO/context.xml

maxIdleSwap="1" How long is it not used

directory

HttpSessionBindingListener (bind and unbind session)

You need to add a binding interface to any class in the new session.

session.setAttribute("p",new Person(1,"tom"));

To add an interface to the Person class

Let the container inherit the listener, then add @WebListener

@WebListener

public class SessionListener implements HttpSessionListener, HttpSessionIdListener{}

web.xml Register

me.liangtian.web.servlet.myServletLis

me.liangtian.web.servlet.myRequestLis

me.liangtian.web.servlet.mySessionLis

Thank you for reading, the above is the "Litener listener principle and role is what" content, after the study of this article, I believe that we have a deeper understanding of the principle and role of Litener listener, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Listener listener interface event action principle disk three object learning content container time time state configuration different no business code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information vpn Linux macOS