Statistics on sessions, sessionlistener, and online users in javaweb

Source: Internet
Author: User

Test environment: Tomcat 5.0.28

Tomcat with more than 5.5 is required, and a bug occurs at 5.5, and sessiondestroyed runs twice.

Sessions in Java Web

  • In Java Web, an httpsession (Session) object is used to represent a session.
  • Session creation (beginning of the session cycle): When the request. getsession () method is called for the first time, a session object is created.
  • Session destruction (indicating the end of the session cycle): the session is called within a request cycle. invalidate () method. After the request cycle ends, the session is destroyed, or it is automatically destroyed after the session times out.
  • For JSP, if<% @ Page session = "false" %>The built-in session variables cannot be directly accessed in JSP, and the session is not automatically created because the JSP does not automatically execute the request. getsession () operation to obtain the session.
  • In the first request cycle just created for the session object, call session. isnew () to obtain true.
  • You can configure the default Session Timeout (minutes) in the web. xml file ):<Session-config>
    <Session-Timeout>10</Session-Timeout>
    </Session-config>

    You can also call session. setmaxinactiveinterval () to set the Session Timeout time (minutes)

Sessionlistener

  • you can use sessionlistenr to listen to the creation and destruction of sessions. Steps:
    1. write a class mysessionlistener to implement javax. servlet. HTTP. httpsessionlistener interface and its sessioncreated () and sessiondestroyed () methods
    2. in the web. configure sessionlistener in XML: listener >
    listener-class > full name of mysessionlistener class listener-class >
    listener >
  • When the session is created and destroyed, the container will call the sessioncreated () method and sessiondestroyed () method of sessionlistener respectively. In the two methods, a parameter object httpsessionevent is input, you can use the getsession () method of this object to obtain the session object.

Application: Online count statistics

Import Java. util. hashset;
Import Javax. servlet. servletcontext;
Import Javax. servlet. http. httpsession;
Import Javax. servlet. http. httpsessionevent;
Import Javax. servlet. http. httpsessionlistener;
 
Public   Class Mysessionlistener Implements Httpsessionlistener {
 
Public   Void Sessioncreated (httpsessionevent event ){
Httpsession = Event. getsession ();
Servletcontext Application = Session. getservletcontext ();

// All sessions are saved by a hashset in the application range.
Hashset sessions = (Hashset) application. getattribute ( " Sessions " );
If (Sessions =   Null ){
Sessions =   New Hashset ();
Application. setattribute ( " Sessions " , Sessions );
}

// All newly created sessions are added to the hashset set.
Sessions. Add (session );
// The sessions set can be retrieved from the application range elsewhere. // Use sessions. Size () to obtain the number of sessions in the current activity, that is, "Number of online users"
}
 
Public   Void Sessiondestroyed (httpsessionevent event ){
Httpsession = Event. getsession ();
Servletcontext Application = Session. getservletcontext ();
Hashset sessions = (Hashset) application. getattribute ( " Sessions " );

// All destroyed sessions are removed from the hashset set.
Sessions. Remove (session );
}
}

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.