In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the Listener interfaces in Servlet". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the Listener interfaces in Servlet"?
Event handling in / Servlet anyone who has written AWT or Swing programs must be impressed by the event handling mechanism of desktop programs: classes that implement the Listener interface can respond to specific events by calling specific methods when a particular event (Event) occurs.
In fact, when we write JSP/Servle programs, we also have a similar event handling mechanism, except that Listener is registered in web.xml in JSP/Servlet, and Container calls specific classes that implement Listener when specific events occur.
1. Listener and Event in Servlet:
In JSP 2.0/Servlet 2.4, there are eight Listener interfaces and six Event categories.
L ServletContextListener
[interface method] contextInitialized () and contextDestroyed ()
[receive event] ServletContextEvent
[trigger scenario] when Container loads a Web application (for example, after starting Container), it calls contextInitialized (), and when the container removes the Web application, it calls the contextDestroyed () method.
L ServletContextAttributeListener
[API method] attributeAdded (), attributeReplaced (), attributeRemoved ()
[receive event] ServletContextAttributeEvent
[trigger scenario] if an object is added as an attribute of an application (ServletContext) object, attributeAdded () is called. Similarly, attributeReplaced () and attributeRemoved () are called when the attribute is replaced and the attribute is removed.
L HttpSessionListener
[interface method] sessionCreated () and sessionDestroyed ()
[receive event] HttpSessionEvent
[trigger scenario] when a session (HttpSession) object is created or destroyed, these two methods are called respectively.
L HttpSessionAttributeListener
[API method] attributeAdded (), attributeReplaced (), attributeRemoved ()
[receive event] HttpSessionBindingEvent
[trigger scenario] if an object is added as an attribute of a session (HttpSession) object, attributeAdded () is called. Similarly, attributeReplaced () and attributeRemoved () are called when the attribute is replaced and the attribute is removed.
L ServletRequestListener
[interface method] requestInitialized () and requestDestroyed ()
[receive event] RequestEvent
[trigger scenario] when a request (HttpServletRequest) object is created or destroyed, these two methods are called respectively.
L ServletRequestAttributeListener
[API method] attributeAdded (), attributeReplaced (), attributeRemoved ()
[receive event] HttpSessionBindingEvent
[trigger scenario] if an object is added as an attribute of a request (HttpServletRequest) object, attributeAdded () is called. Similarly, attributeReplaced () and attributeRemoved () are called when the attribute is replaced and the attribute is removed.
L HttpSessionBindingListener
[interface method] valueBound () and valueUnbound ()
[receive event] HttpSessionBindingEvent
[trigger scenario] implements the category of the HttpSessionBindingListener API. If its instance is added to the attribute of the session (HttpSession) object, it will call valueBound (). If it is removed from the attribute of the session (HttpSession) object, it will call valueUnbound (). The category that implements the HttpSessionBindingListener interface does not need to be set in web.xml.
L HttpSessionActivationListener
[interface method] sessionDidActivate () and sessionWillPassivate ()
[receive event] HttpSessionEvent
[trigger scenario] Activate and Passivate are actions used to replace objects. When an session object must be temporarily stored to a hard disk or other storage for reasons such as resource utilization or load balancing (through object serialization), the action is called Passivate, and the action taken when the session object on the hard disk or storage reloads JVM is called Activate, so it is easy to understand that sessionDidActivate () and sessionWillPassivate () are called after Activeate and before Passivate, respectively.
two。 Register listener
In addition to HttpSessionBindingListener and HttpSessionActivationListener, you must register with the container in web.xml before the container calls the corresponding category when the corresponding event occurs, such as:
Demo.servlet.listener.CustomServletContextListener listener-class > listener > Thank you for your reading. The above is the content of "what are the Listener interfaces in Servlet". After the study of this article, I believe you have a deeper understanding of what the Listener interfaces in Servlet have, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.