在最近學習的session做用域中,順便了解了一下cookie,javascript
session是存放在服務器中,而cookie是存放在客戶端中。css
本篇文章主要是使用cookie來實現記住密碼的功能。html
簡單的login.jsp頁面的代碼: java
在這裏解釋一下第二行的s標籤,我是使用struts2框架作的。就簡單的servelet和jsp能夠實現一樣的功能,jquery
既然能夠記住密碼,相應的就是密碼安全問題。在這裏順便說一下MD5加密。值須要一個md5.js就能夠sql
login.jsp數據庫
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 14 <title>My JSP 'login.jsp' starting page</title> 15 16 <meta http-equiv="pragma" content="no-cache"> 17 <meta http-equiv="cache-control" content="no-cache"> 18 <meta http-equiv="expires" content="0"> 19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 20 <meta http-equiv="description" content="This is my page"> 21 <!-- 22 <link rel="stylesheet" type="text/css" href="styles.css"> 23 --> 24 <link rel="stylesheet" href="css/style_public.css" /> 25 <link rel="stylesheet" href="css/style_login.css" /> 26 <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script src="js/md5.js" ></script> 27 </head> 28 29 <body> 30 ta<!--頭部--> 31 <header> 32 <div class="btn"><a href="login.html">登錄</a>/<a href="registered.html">註冊</a></div> 33 </header> 34 <!--中間內容(主題內容)--> 35 <div class="main"> 36 <div class="box"> 37 <p><a href="index.html">返回首頁</a></p> 38 <form id="forml" action="user_valid.action" method="post"> 39 <s:token></s:token> 40 <div class="content"> 41 <p>QQ號</p> 42 <input type="text" placeholder="請輸入QQ號" id="input1" name="um.userid" /> 43 <p>密碼</p> 44 <input type="password" placeholder="請輸入密碼" id="input2" name="um.userpwd" onblur="getmd5()"/> 45 <div style="margin-top: 20px;color:red;" ><input id="remebers" type="checkbox" name="remenber" value="1"/> 46 <span >記住用戶名和密碼</span></div> 47 <div class="btnss" onclick="sub()">登陸</div> 48 </div> 49 </form> 50 <span id="sp1"></span> 51 <span id="sp2"></span> 52 </div> 53 54 </div> 55 <!--尾部--> 56 <footer> 57 <p style="margin-top: 0px;">網站連接:你猜網 www.xxxxxxxxx.com</p> 58 </footer> 59 <script> 60 $(document).ready(function () { 61 $("#input1").focus(); 62 //記住用戶名和密碼 63 $('#remebers').click(function () { 64 if ($("#input1").val() == "") { 65 alert("用戶名不能爲空!"); 66 } 67 if($("#input2").val() == "") 68 { 69 alert("密碼不能爲空!"); 70 } else { 71 if ($('#remebers').attr("checked")) { 72 setCookie("userid", $("#input1").val(), 60); 73 setCookie("upwd", $("#input2").val(), 60); 74 }else { 75 delCookie("userid"); 76 delCookie("upwd"); 77 } 78 } 79 }); 80 if (getCookie("userid") != null) 81 { 82 $('#remebers').attr("checked", "checked"); 83 $('#input1').val(getCookie("userid")); 84 $('#input2').val(getCookie("upwd")); 85 } 86 }) 87 //寫cookies 88 function setCookie(name, value) { 89 var Days = 30; 90 var exp = new Date(); 91 exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); 92 document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); 93 } 94 //讀取cookies 95 function getCookie(name) { 96 var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); 97 if (arr = document.cookie.match(reg)) return unescape(arr[2]); 98 else return null; 99 } 100 //刪除cookies 101 function delCookie(name) { 102 var exp = new Date(); 103 exp.setTime(exp.getTime() - 1); 104 var cval = getCookie(name); 105 if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); 106 } 107 108 109 110 function sub(){ 111 document.getElementById("forml").submit(); 112 } 113 114 window.onload=function(){ 115 var input1=document.getElementById("input1"); 116 var input2=document.getElementById("input2"); 117 var sp1=document.getElementById("sp1"); 118 var sp2=document.getElementById("sp2"); 119 //失去焦點時判斷 QQ號 120 input1.onblur=function(){ 121 if(sp1.innerText==""||sp1.innerText==null||sp1.innerText==undefined){ 122 sp1.innerText="請輸入您的QQ號"; 123 } 124 } 125 //失去焦點時判斷密碼 126 input2.onblur=function(){ 127 if(sp2.innerText==""||sp2.innerText==null||sp2.innerText==undefined){ 128 sp2.innerText="請輸入您的密碼"; 129 } 130 } 131 }
//將密碼生成md5,密碼輸完失去焦點時調用
function getmd5(){
var a= hex_md5($("#input2").val())apache
$("#input2").val(a);安全
}服務器
132 </script> 133 </body> 134 </html>
頁面佈局差很少了,那就是後臺,分別得到input中的三個值,登陸id和密碼還有是否記住密碼的選框
1 package com.direct.action; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 9 import javax.servlet.http.Cookie; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.apache.struts2.ServletActionContext; 14 15 import com.direct.dao.LoginDao; 16 import com.direct.model.UserModel; 17 import com.direct.util.Dutil; 18 import com.opensymphony.xwork2.ActionContext; 19 import com.opensymphony.xwork2.ActionSupport; 20 21 public class Login extends ActionSupport{ 22 23 private UserModel um; 24 private int remenber; 25 26 public String valid(){ 27 LoginDao dao = new LoginDao(); 28 ArrayList<UserModel> listum = dao.vali(um.getUserid(), um.getUserpwd()); 29 if (listum.size()>0) { 30 ActionContext.getContext().getSession().put("um", listum.get(0));// 一次請求對象存入做用域 31 //處理Cookie: name:userInfo 32 Cookie c=new Cookie("userid", um.getUserid()+""); 33 HttpServletRequest request = ServletActionContext.getRequest(); 34 HttpServletResponse response = ServletActionContext.getResponse(); 35 c.setPath(request.getContextPath()); 36 if(remenber==0){ 37 //Cookie 時間 38 c.setMaxAge(0); 39 }else{ 40 //記住Cookie 41 c.setMaxAge(60*60);//保存時間 一分鐘 42 } 43 response.addCookie(c);//將Cookie相應到客戶端 44 45 46 }else { 47 return "loginhtml"; 48 } 49 50 return "login"; 51 } 52 public UserModel getUm() { 53 return um; 54 } 55 public void setUm(UserModel um) { 56 this.um = um; 57 } 58 public int getRemenber() { 59 return remenber; 60 } 61 public void setRemenber(int remenber) { 62 this.remenber = remenber; 63 } 64 65 66 }
再說一遍,我使用的struts2框架,後臺的實現和servlet有點區別,但大體相同。先獲取到值,鏈接數據庫判斷是否登陸成功。
這裏的um是一個實體對象,就是用戶基本信息的對象。在這裏我就沒有寫。
還有就是驗證用戶名和密碼數據庫是否存在的dao方法,我也沒有貼出。簡單的望你們自行解決,不會的留言討論。
再判斷用戶是否使用了記住密碼,記住了密碼則生產cookie對象,存放數據。不然清空cookie存放的數據。
記住密碼後在客戶端是能夠看到信息的。