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

How does Listener listen to Http Session?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Listener how to monitor Http Session, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Listener is the listener of Servlet, it can listen to client's request, server's operation and so on. Through the listener, you can automatically trigger some actions, such as monitoring the number of users online. When you add a Http Session, the session Created (Http Session Event se) method is fired, which adds 1 to the number of people online. The common monitoring APIs are as follows:

Servlet Context Attribute Listener listens for operations on Servlet Context properties, such as adding, deleting, and modifying properties.

Servlet Context Listener listens on Servlet Context. The Context Initialized (Servlet Context Event sce) method fires when the Servlet Context is created, and the Context Destroyed (Servlet Context Event sce) method is fired when the Servlet Context is destroyed.

Http Session Listener listens for the operation of Http Session. The session Created (Http Session Event se) method is fired when a Session is created, and the session Destroyed (Http Session Event se) method is fired when a Session is destroyed.

Http Session Attribute Listener listens for the operation of properties in Http Session. The attribute Added (Http Session Binding Event se) method is fired when an attribute is added to Session, the attribute Removed (Http Session Binding Event se) method is fired when an attribute is deleted in Session, and the attribute Replaced (Http Session Binding Event se) method is fired when the Session property is reset.

Let's develop a specific example, this listener can count the number of people online. When the Servlet Context is initialized and destroyed, the corresponding information is printed on the server console. When the attributes in Servlet Context are added, changed, or deleted, the corresponding information is printed in the server console.

To achieve the above functions, the listener must implement the following three interfaces:

◆ HttpSessionListener

◆ Servlet Context Listener

◆ Servlet Context AttributeListener

Import javax.servlet.http.*; import javax.servlet.*; public class OnLineCountListener implements HttpSessionListener, ServletContextListener,ServletContextAttributeListener {private int count; private ServletContext context = null; public OnLineCountListener () {count=0; / / setContext ();} / / fire public void sessionCreated (HttpSessionEvent se) {count++; setContext (se) when creating a session;} / / fire public void sessionDestroyed (HttpSessionEvent se) {count--; setContext (se) when a session fails } / / sets the property of context, which will fire the attributeReplaced or attributeAdded method public void setContext (HttpSessionEvent se) {se.getSession (). GetServletContext (). SetAttribute ("onLine", new Integer (count));} / / fires public void attributeAdded (ServletContextAttributeEvent event) {log ("attributeAdded ('" + event.getName () + ",'" + event.getValue () + "") "when adding a new attribute) } / / fire public void attributeRemoved (ServletContextAttributeEvent event) {log ("attributeRemoved ('" + event.getName () + ",'" + event.getValue () + "')) when a new attribute is deleted; public void attributeReplaced (ServletContextAttributeEvent event) {log (" attributeReplaced ('"+ event.getName () +",'"+ event.getValue () +")") when the attribute is replaced. } / / fire public void contextDestroyed (ServletContextEvent event) {log ("contextDestroyed ()") when context is deleted; this.context = null;} / / public void contextInitialized (ServletContextEvent event) {this.context = event.getServletContext (); log ("contextInitialized ()");} private void log (String message) {System.out.println ("ContextListener:" + message);}

In OnLine Count Listener, the number of people currently online is represented by count, and OnLine Count Listener will be executed automatically when the Web server starts. When the OnLine Count Listener is constructed, set the count to 0. The session Created (Http Session Event se) method is automatically called with each additional Session,OnLine Count Listener, and the session Destroyed (Http Session Event se) method is automatically called with each Session,OnLine Count Listener destroyed. When the session Created (Http Session Event se) method is called, it means that another customer is requesting, so increase the number of people online (count) by 1 and write the count to the Servlet Context. Servlet Context information is shared by all clients so that each client can read the number of people currently online.

In terms of scope, the scope of Servlet includes Servlet Context, Http Session,Servlet Request.

After reading the above, have you mastered how Listener monitors Http Session? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Development

Wechat

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

12
Report