Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle and function of Litener listener?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report