Determine whether a user is logged on and solve the problem of repeated User Logon

Source: Internet
Author: User

Solve the Problem of User Logon: (write a filter)

Public class check_user_loginfilter implements filter {

Protected filterconfig;

Public void Init (filterconfig arg0) throws servletexception {
// Todo auto-generated method stub
This. filterconfig = arg0;
}

Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
// Todo auto-generated method stub
Httpservletrequest sr = (httpservletrequest) request;
Httpsession session = Sr. getsession (); // GET THE SESSION
Register User = (Register) Session. getattribute ("userinfo ");
Sessionlistener SL = new sessionlistener ();

 

// Determine whether the user is logged on. If not,
If (user = NULL | user. Equals ("")){

// Obtain the current URL of the token
String strurl = Sr. getrequesturl (). tostring ();
// Determine whether it is index. jsp or userlogin. Do
If (strurl. indexof ("/index. jsp") =-1 ){
If (strurl. indexof ("/userlogin. Do") =-1 ){
Servletcontext SC = filterconfig
. Getservletcontext ();
Requestdispatcher RD = SC
. Getrequestdispatcher ("/work/index. jsp ");
Try {
Rd. Forward (request, response); // forward to index. jsp for User Login
// Chain. dofilter (request, response );
Return;
} Catch (servletexception SX ){
Filterconfig. getservletcontext (). Log (
SX. getmessage ());
} Catch (ioexception iox ){
Filterconfig. getservletcontext (). Log (
Iox. getmessage ());
}
}
}
}
Chain. dofilter (request, response );

}

Public void destroy (){
// Todo auto-generated method stub
This. filterconfig = NULL;
}

}

<! -- Webxml judge User Logon -->

<Filter>
<Filter-Name> check_user_loginfilter </filter-Name>
<Filter-class>
Com. WP. App. util. check_user_loginfilter
</Filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> check_user_loginfilter </filter-Name>
<URL-pattern> *. jsp </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> check_user_loginfilter </filter-Name>
<URL-pattern> *. DO </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> check_user_loginfilter </filter-Name>
<URL-pattern> *. html </url-pattern>
</Filter-mapping>

Solve the Problem of repeated User Logon: (configure listener implementation. If you log on repeatedly, t will be dropped from the previous user)

Public class sessionlistener implements httpsessionlistener {
Private Static java. util. hashtable husername = new hashtable ();
Public void sessioncreated (httpsessionevent SE ){
System. Out. println ("a new user session has been created --" + Se );
}

Public void sessiondestroyed (httpsessionevent SE ){
Husername. Remove (SE. getsession ());
System. Out. println ("delete user session --" + Se );
}

Public synchronized static Boolean islogined (httpsession session, string susername ){
Boolean flag = false;
If (husername. containskey (susername )){
Flag = true;
Httpsession vsession = (httpsession) husername. Get (susername );
Try {
Vsession. invalidate ();
System. Out. println ("user-" + susername + "| the session already exists-" + session );
System. Out. println ("Set-" + susername + "| session expired and removed ");
}
Catch (exception ex ){}
}
Else {
Flag = false;

}
Husername. Remove (susername );
Husername. Put (susername, session );
System. Out. println ("create user-" + susername + "| session-" + session );

Return flag;
}

Public synchronized static Boolean deleteusername (httpsession session, string susername)
{
System. Out. println ("delete user-" + susername + "| session-" + session );
Husername. Remove (susername); // delete a user
Return true;
}

Public synchronized static Boolean isonline (httpsession session, string susername ){
Boolean flag = false;
Httpsession vsession = NULL;
Try {
Vsession = (httpsession) husername. Get (susername );
} Catch (exception e ){

}
If (vsession! = NULL ){
If (session. GETID (). Equals (vsession. GETID ())){
Flag = true;
} Else {
Flag = false;
}
}
Return flag;

}
}

 

<! -- Webxml configuration listener -->
<Listener>
<Listener-class> com. Lihong. WP. Web. Action. loginaction. sessionlistener </listener-class>
</Listener>

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.