index.jsphtml
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@page import="java.net.*" %> <%@page import="comm.MakeMD5" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>MyIndex</title> <meta http-equiv="imurl" content="no-cache"> </head> <body> <% boolean loginFlag=false; String account=null; String md5Account=null; Cookie cookieArr[]=request.getCookies(); if(cookieArr!=null&&cookieArr.length>0){ for(Cookie cookie:cookieArr){ if(cookie.getName().equals("account")){ account=cookie.getValue(); account=URLDecoder.decode(account,"utf-8"); //System.out.print(account); } if(cookie.getName().equals("md5Account")){ md5Account=cookie.getValue(); md5Account=URLDecoder.decode(md5Account,"utf-8"); //System.out.print(md5Account); } } } if(account!=null&&md5Account!=null){ loginFlag=md5Account.equals(MakeMD5.getMD5(account)); } if(loginFlag){ //request.getRequestDispatcher("successlogin.jsp").forward(request, response); //response.sendRedirect("successlogin.jsp"); %> <fieldset> <legend>歡迎您回來</legend> <table align="center"> <tr> <td><%=account %>,歡迎您登錄本網站</td> <td align="center"> <a href="foreverlogin?action=logout">註銷登錄</a> </td> </tr> </table> </fieldset> <% }else{ %> <fieldset> <legend>用戶登陸</legend> <form action="foreverlogin?action=login" method="post"> <table> <tr> <td>帳 號:</td> <td><input type="text" name="account"></td> </tr> <tr> <td>密 碼:</td> <td><input type="text" name="password"></td> </tr> <tr> <td>有效期:</td> <td> <input type="radio" name="timeout" value="-1" checked="checked"> 關閉瀏覽器即失效 <input type="radio" name="timeout" value="<%=30*24*60*60%>"> 30天內有效 <input type="radio" name="timeout" value="<%=Integer.MAX_VALUE%>"> 永久有效 </td> </tr> <tr> <td> <input type="submit" value="登錄"> <input type="reset" value="重置"> </td> </tr> </table> </form> </fieldset> <% } %> </body> </html>
src/comm/foreverlogin.javajava
package comm; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class foreverlogin extends HttpServlet { private static final long serialVersionUID = 1L; public foreverlogin() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String action=request.getParameter("action"); if(action.equals("login")){ login(request,response); } else if(action.equals("logout")){ logout(request,response); } } //login public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ String account=request.getParameter("account"); //String password=request.getParameter("password"); int timeout=Integer.parseInt(request.getParameter("timeout")); String md5Account=MakeMD5.getMD5(account); //採用MD5算法加密 account=URLEncoder.encode(account,"utf-8"); //帳號爲中文時須要轉換Unicode才能保存在Cookie中 Cookie accountCookie=new Cookie("account",account); accountCookie.setMaxAge(timeout); Cookie md5AccountCookie=new Cookie("md5Account",md5Account); md5AccountCookie.setMaxAge(timeout); response.addCookie(accountCookie); response.addCookie(md5AccountCookie); //將線程休眠1秒後在執行 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //response.sendRedirect("cookie/resultlogin.jsp?"+System.currentTimeMillis()); response.sendRedirect("cookie/index.jsp?"+System.currentTimeMillis()); } //logout public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ Cookie accountCookie=new Cookie("account",""); accountCookie.setMaxAge(0); Cookie md5AccountCookie=new Cookie("md5Account",""); md5AccountCookie.setMaxAge(0); response.addCookie(accountCookie); response.addCookie(md5AccountCookie); //將線程休眠一秒後在執行 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.sendRedirect("cookie/index.jsp?"+System.currentTimeMillis()); } public void init() throws ServletException { // Put your code here } }
src/comm/MakeMD5.java算法
package comm; import java.security.MessageDigest; public class MakeMD5 { public final static String getMD5(String str){ // 用來將字節轉換成 16 進製表示的字符 char hexDiagiArr[]={'0','1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f'}; MessageDigest digest=null; try{ digest=MessageDigest.getInstance("MD5"); //建立MD5算法摘要 digest.update(str.getBytes()); //更新摘要 byte mdBytes[]=digest.digest(); //加密,並返回字節數組 //新建字符數組,長度爲myBytes字節數組的2倍,用於保存加密後的值 char newCArr[]=new char[mdBytes.length*2]; int k=0; for(int i=0;i<mdBytes.length;i++){ byte byte0=mdBytes[i]; newCArr[k++]=hexDiagiArr[byte0>>>4&0x0f]; //取字節中高 4 位的數字轉換,>>>爲邏輯右移,將符號位一塊兒右移 newCArr[k++]=hexDiagiArr[byte0&0x0f]; //取字節中低 4 位的數字轉換 //針對字符0-9的,0-9的ascii碼值爲0x30,0x31,0x32 0x33 ...0x39, //所以與0x0f按位與後只保留個位上的書即0x0,0x1,。。。0x9 // 0000 1010 //& 0000 1111 // 0000 1010 } return String.valueOf(newCArr); //將轉換後的字符轉換爲字符串 } catch(Exception e){ e.printStackTrace(); } return null; } }