Servlet listeners and online users

Source: Internet
Author: User

Servlet listeners are divided into three types
I listen to creation and destruction of ServletContext, Request, and Session scopes
(1) ServletContextListener
(2) HttpSessionListener
(3) ServletRequestListener
II listen to attributes changes in the ServletContext, Request, and Session scopes (ADD, modify, and delete)
(1) ServletContextAttributeListener
(2) HttpSessionAttributeListener
(3) ServletRequestAttributeListener
The above two listeners must be registered in web. xml.
<Listener>
<Listener-class> com. hitech. listener. _ ServletContextListener </listener-class>
</Listener>
III listen for changes in the object status in HttpSession (bound, unbound, inactive, and deactivated)
(1) HttpSessionBindingListener perceives binding and unbinding
(2) HttpSessionActivationListener perceives changes in passivation and activation status
The Serializable mark interface must be implemented for object passivation and activation.
This type of listener does not need to be configured in web. xml, which is usually the JavaBean implementation interface.

Implementation of the function of displaying online users:
JavaBean -- UserBean implements HttpSessionListener that encapsulates user information
1. User Logon:
Search for matched user information in the database based on the user name and password entered by the user. encapsulate the information as a UserBean object and return it to the Session scope to save the user's logon status.
2. Data Structure for storing User Logon status
When a Web application is loaded, a Map that stores user information and corresponding sessions is created. The ServletContextListener listener is used.
Override the public void contextInitialized (ServletContextEvent sce) method. You can obtain the ServletContext object through the sce object.
Map <UserBean, HttpSession> userMap = new HashMap <UserBean, HttpSession> ();
Sce. getServletContext. setAttribute ("userMap", userMap );
3. UserBean implements the HttpSessionListener Interface
Override UserBean after implementing the HttpSessionListener Interface
Public void valueBound (HttpSessionBindingEvent event) -- triggered when the UserBean object is stored in the Session domain
This method is automatically triggered when a user logs on. You can add a user to the online user list.
Use event. getSession. getServletContext. getAttribute ("userMap") to obtain the userMap stored in the ServletContext domain.
UserMap. put (this, event. getSession () stores the currently logged-on users and their sessions in Map.
Public void valueUnbound (HttpSessionBindingEvent event) -- triggered when the UserBean object is removed from the Session domain
This function is automatically triggered when the user logs out or the Session times out. The user's Session can be automatically removed.
4. Possible problems:
(1) The user will log on twice
When a user logs on, check whether the user has logged on in Map. if the user has logged on, remove the previous Session before logging on.
If (userMap. containsKey (bean )){
UserMap. get (bean). invalidate (); // destroy the previous Session
}
(2) but HaspMap compares whether two bean objects are the same and relies on the hashCode () and equals () methods. Therefore, UserBean needs to override these two methods to ensure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.