Statistics on the number of online users (the number of sessions obtained from Tomcat)

Source: Internet
Author: User

Let alone directly add the Code:

Import org. Apache. Catalina. context;
Import org. Apache. Catalina. Manager;
Import org. Apache. Catalina. connector. request;
Import org. Apache. Catalina. connector. requestfacade;


Code:

Int activesessions = 0;
If (request instanceof requestfacade ){
Field requestfield = request. getclass (). getdeclaredfield ("request ");
Requestfield. setaccessible (true );
Request Req = (request) requestfield. Get (request );
Context context = Req. getcontext ();
Manager = context. getmanager ();
Activesessions = manager. getactivesessions ();
}

Catalina. Jar under tomcat/lib is referenced during compilation. After the war package is compiled, remember to delete Catalina. jar from the war package.

At this time, the advantages of Maven come, scope writes a provided (the container already provides) is OK.




Principle:

Tomcat's implementation of request (javax. servlet. http. httpservletrequest) is org. Apache. Catalina. connector. requestfacade.

Requestfacade has a protected request (Org. Apache. Catalina. connector. Request) attribute.

Request can get context (Org. Apache. Catalina. Context)

Context can get the manager (Org. Apache. Catalina. Manager, Tomcat manages session classes)

Then, the manager obtains the number of active sessions, that is, the number of online users.


Statistics on the number of online users (the number of sessions obtained from Tomcat)

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.