Step 1: once the [automatic logon] option is selected during login, the following code must be appended after the login is successful. generally, the website provides the function of saving the user name, so I wrote this out. Only passwords are handled separately.
The host is your domain name.
Login. jsp
The code is as follows: |
Copy code |
String host = request. getServerName (); Cookie cookie = new Cookie ("SESSION_LOGIN_USERNAME", username); // save the user name to Cookie Cookie. setPath ("/"); Cookie. setDomain (host ); Cookie. setMaxAge (99999999 ); Response. addCookie (cookie ); If (ParamUtils. getBooleanParameter (request, "savePassword ")){ // Save the password to the Cookie. Be sure to encrypt it. Cookie = new Cookie ("SESSION_LOGIN_PASSWORD", MD5.encode (u. getPassword ())); Cookie. setPath ("/"); Cookie. setDomain (host ); Cookie. setMaxAge (99999999 ); Response. addCookie (cookie ); } |
In this way, the Cookie is generated
Step 2: When the user accesses the website, if the user detects that the user has not logged on, the following judgment will be made.
The code is as follows: |
Copy code |
Index. jsp String usernameCookie = null; String passwordCookie = null; Cookie [] cookies = request. getCookies (); If (cookies! = Null ){ For (Cookie cookie: cookies ){ If ("SESSION_LOGIN_USERNAME". equals (cookie. getName ())){ UsernameCookie = cookie. getValue (); // Obtain the cookie username } If ("SESSION_LOGIN_PASSWORD". equals (cookie. getName ())){ PasswordCookie = cookie. getValue (); // Obtain the cookie password. } Www.111cn.net } If (usernameCookie! = Null & passwordCookie! = Null) {// if Yes If (Login. checkLogin (usernameCookie, passwordCookie )){ // Process successful login } Else { // Handle unsuccessful login } } } |
Is it easy?