Java uses the SSM framework to implement the login page remember password function

Source: Internet
Author: User
Tags set cookie

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

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.