JSP online user count statistics-implement forced exit of a user on the background

Source: Internet
Author: User

This is a stupid method .. I Know That tomcat has a way to get the system session .. however, I cannot obtain the related classes with my abilities... if some experts see this .... tell me what you know ....

 

Countonlineuser. Java
/*
Power by 24 comic
DESIGN: Lulu

Principle
Build the listener. When a session named jvod_user is created, the listener is triggered and the method is called.
Read the hashmap object named Olu in the application. The value of each hashmap is a useritem object in my program.
Add the sessionid of the user session to Olu... Put (username, sessionid)
Then save the entire hashmap object to the application...

When sessiong expires or is destroyed, the key of the Olu object is deleted.

E-mail: smildlzj@hotmail.com
*/

Package com. jvod. system;
Import java. util. hashmap;

Import javax. servlet .*;
Import javax. servlet. http .*;

Import com. jvod. item. useritem;

Public class countonlineuser implements httpsessionattributelistener
{
Public countonlineuser ()
{
// System. Out. println ("------- init count ");
}
Public void attributeadded (httpsessionbindingevent SE)
{
Try {
// System. Out. println ("------- se. getname ():" + Se. getname ());
If (SE. getname (). Equals ("jvod_user "))
{
Httpsession session = Se. getsession ();
// System. Out. println ("------- session creation:" + session. GETID ());
Servletcontext SC = session. getservletcontext ();
Hashmap Olu = (hashmap) SC. getattribute ("onlineuser ");
Useritem = (useritem) se. getvalue ();
If (useritem! = NULL) // It is a logged-on user.
{
If (Olu. Get (useritem. getname ())! = NULL) // previously logged on again
{
// Obtain the previous sessionid
String sessionid = (string) Olu. Get (useritem. getname ());
// Destroy the session that the user logs on elsewhere
// Call remove
// System. Out. println ("------- call remove same session ");
Sessionmanage Sm = new sessionmanage ();
SM. setservletcontext (SC );
// System. Out. println ("------- result of remove:" + SM. sessionremove (sessionid ));
}

// Add a new sessionid
Olu. Put (useritem. getname (), session. GETID ());
SC. setattribute ("onlineuser", Olu );
}
}
} Catch (exception e ){
System. Out. println (E. tostring ());
}

}

Public void attributeremoved (httpsessionbindingevent SE)
{
Try {
If (SE. getname (). Equals ("jvod_user "))
{
Httpsession session = Se. getsession ();
// System. Out. println ("------- session destruction:" + session. GETID ());
Servletcontext SC = session. getservletcontext ();
Hashmap Olu = (hashmap) SC. getattribute ("onlineuser ");
Useritem = (useritem) se. getvalue ();
If (useritem! = NULL) // It is a logged-on user.
{
Olu. Remove (useritem. getname ());
SC. setattribute ("onlineuser", Olu );
}
}
} Catch (exception e ){
System. Out. println (E. tostring ());
}
}

Public void attributereplaced (httpsessionbindingevent arg0 ){
// Todo auto-generated method stub

}

}

Then use java.net. httpurlconnection to simulate the client.

Public Boolean sessionremove (string sessionid)
{
Try {

..................
URL url = new URL (the session page is destroyed by the foreground user)
Httpurlconnection conn = NULL;
Conn = (httpurlconnection) URL. openconnection (); |
Conn. setrequestproperty ("cookie", "JSESSIONID =" + sessionid + ";"); // you can specify the session ID of a user.

.......

}

Send the user session and simulate the client to access the page containing session. removeattribute ("jvod_user"); to complete the force exit function.

 

The background page is to read the Olu object in the application and traverse the hashmap to get the user list... and sessionid. After processing the exit page, you can call the above functions...

 

Related Article

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.