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...