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)