The final effect shows:
1. Login Page JSP code
<label class= "checkbox" > <input type= "checkbox" Name= "RememberMe" id= "RememberMe" onclick= "Remember ();" /> Remember Password </label>
Select Remember password trigger event, assign value to 1 if checked, or 0 function remember () { var Remflag = $ ("Input:checkbox"). Is (': checked '); if (remflag) { //cookie the user name and password, the echo is the real user name and password, there is a security issue . var conflag = confirm ("Record password function should not be used in public places in order to prevent password leakage." Are you sure you want to use this feature? "); if (conflag) { //Confirm flag $ ("#rememberMe"). Val ("1"); } else{ $ ("Input:checkbox"). Removeattr (' checked '); $ ("#rememberMe"). Val ("0"); } } else{ //If set Remflag is not selected as "" $ ("#rememberMe"). Val ("0");} }
Start function to get the user information saved in the cookie
$ (function () {
The cookie Data preservation format is key=value;key=value; form, Logininfo is the key value stored in the cookie, specifically see Controller code var str = GetCookie (" Logininfo "); str = str.substring (1,str.length-1); var username = str.split (",") [0]; var password = str.split (",") [1]; Auto-populate user name and password $ ("#username"). Val (username); $ ("#password"). val (password);
});
Get cookie function GetCookie (CNAME) { var name = cname + "="; var ca = Document.cookie.split (';'); for (var i=0; i<ca.length; i++) { var c = ca[i]; while (C.charat (0) = = ") c = c.substring (1); if (C.indexof (name)! =-1) return c.substring (Name.length, c.length); } Return ""; }
2. Controller code after login request
Create cookie object, save user information, set cookie expiration time, add cookie via response
if ("1". Equals (Cookieflag)) { String Logininfo = Username + "," + password; Cookie Usercookie = new Cookie ("Logininfo", logininfo); Usercookie.setmaxage (1 * 24 * 60 * 60); Survival period is one day 1*24*60*60 usercookie.setpath ("/"); Res.addcookie (Usercookie);}
Java uses the SSM framework to implement the login page remember password function