Javaweb use cookies to remember accounts.
First, let's see what the interface looks like.
Remember that the most common practice is to save your account to a cookie when you click Log in.
Material preparation
<script src="${ctx}/background/js/jquery-1.7.2.js" type="text/javascript"></script><script src="${ctx}/background/js/jquery.cookie.js" type="text/javascript"></script>
Go find these two files and introduce them to your page.
Form form
<form Action="${ctx}/j_spring_security_check" method="POST" id="Login_form"> <p> <label>User name:</label> <input type="text" name= "j_username" id="J_ Username " size=" class= "login_input" autocomplete= "Off"/> </P> <p> <label>Secret Code:</label> <input type="password" name="J_password" size=" " Class=" Login_input " /> </P> <p> <input type="checkbox" value="true" id= "J_remember"/><label> </label>Remember my account.</P> <div class="Login_bar"> <input class="Sub" type="Submit" value= "" /> </div> </form>
Note that I used spring's security.
Core content
Create a copy of the login.js, and introduce it in the login.jsp.
varCookie_name =' Sys__username ';$( function() { if($.cookie (Cookie_name)) { $("#j_username"). Val ($.cookie (Cookie_name)); $("#j_password"). focus (); $("#j_remember"). attr (' checked ',true); }Else{ $("#j_username"). focus (); } $("#login_form"). Submit ( function(){ var$remember = $ ("#j_remember");if($remember. attr (' checked ') {$.cookie (Cookie_name, $ ("#j_username"). Val (), {path:'/', Expires: the}); }Else{$.cookie (Cookie_name,NULL, {path:'/'});//Delete cookies} });});
- When the page is loaded, first to infer whether there is a cookie to save, fake, and then remove the user name from the cookie. Appears in the User name input box.
- When the form is submitted, assuming the user chooses to remember the user name, the user name is saved in the cookie, otherwise the cookie information is deleted.
Introduction to Cookie Methods
instance |
method |
Comment |
Create a session Co Okie |
$.cookie (' cookiename ', ' cookievalue '); | The
created cookie expires by default until the user's browser is closed, so it is called a session cookie. |
Create a persistent cookie |
$.cookie (' cookiename ', ' Cookievalue ', {expires:15}); |
is called a persistent cookie when the time is specified, and is valid for a period of 15 days. |
Create a persistent cookie with a valid path |
$.cookie (' cookiename ', ' Cookievalue ', {expires:1 5. Path: '/'}); | The
assumes that a valid path is not set. In the default case. You can only read the Cookie,cookie path on the current page of the cookie setting to set the top-level folder where the cookie can be read. |
get cookie |
$.cookie (' cookiename '); | The
assumes that it is present and returns Cookievalue. Otherwise returns null |
Delete Cookie |
$ . Cookie (' cookiename ', null); |
false assumption to delete a cookie with a valid path. For example, the following: $.cookie (' CookieName ', Null,{path: '/'); |
Summary : Suppose you want to save the password, of course, and the user name almost identical, but you need to jquery.md5.js such a file, used to display your saved password in the Password box.
Javaweb using cookies to remember accounts