利用Servlet实现用户永久登录
生活随笔
收集整理的這篇文章主要介紹了
利用Servlet实现用户永久登录
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Servlet中通過Cookie技術(shù)實(shí)現(xiàn),在Servlet中輸入用戶賬號(hào),密碼和有效期,將賬號(hào)信息保存在Cookie中,設(shè)置Cookie的最大保存時(shí)間,將此Cookie保存在客戶端的Cookie中
使用MD5加密技術(shù),通過MD5加密技術(shù)將用戶賬號(hào)生成一個(gè)密鑰并保存在Cookie中,然后再用戶登錄中,根據(jù)該密鑰來判斷用戶顯示的是用戶登錄還是登陸后的狀態(tài)。MD5加密技術(shù)通過java.security.Message.Digest類實(shí)現(xiàn)的
MakeMD5類,加密
index.jsp頁面,第一次訪問顯示登陸頁面,第二次訪問判斷Servlet返回的Cookie信息,根據(jù)Cookie信息來決定是否顯示用戶登錄之后的信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="com.cn.zj.tool.MakeMD5" %> <%@ page import="java.net.URLDecoder" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><%boolean loginFlag = false; //設(shè)置一個(gè)變量 ,用于保存是否登錄String account = null ; //聲明用于保存從Cookie中讀取的賬號(hào)String md5Account = null; //聲明用于保存從Cookie中讀取的加密的賬號(hào) Cookie cookieArr[] = request.getCookies(); //獲取請(qǐng)求中所有的Cookieif(cookieArr!=null&&cookieArr.length>0){for(Cookie cookie : cookieArr){ //循環(huán)Cookie數(shù)組if(cookie.getName().equals("account")){account = cookie.getValue(); //找到賬號(hào)的Cookie值 account = URLDecoder.decode(account,"UTF-8");//解碼 ,還原中文字符串的值 }if(cookie.getName().equals("md5Account")){md5Account = cookie.getValue(); //找到加密賬號(hào)的Cookie值 }}}if(account!=null&&md5Account!=null){loginFlag = md5Account.equals(MakeMD5.getMD5(account));}%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>利用Cookie實(shí)現(xiàn)永久登錄</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">.style1{width: 400px;height: 200px;border: 1px solid;border-color: green;}table{font-size: 14px;color: navy;font-family: 楷體;}input{font-size: 14px;color: navy;font-family: 楷體;}.btn{font-size: 14px;background-color:orange;color: white;font-family: 楷體;}</style></head><body><%if(loginFlag){%><fieldset class="style1" ><legend>歡迎您回來</legend><table align="center"><tr><td><%=account %>,歡迎您登錄本網(wǎng)站!</td><td align="center"><a href="<%=basePath%>foreverlogin?action=logout">注銷登錄</a></td></tr></table></fieldset><%}else{ %><fieldset class="style1"><legend>用戶登錄</legend><form action="foreverlogin?action=login" method="post"><table align="center"><tr><td>賬號(hào):</td><td><input type="text" name="account"></td></tr><tr><td>密碼:</td><td><input type="password" name="pwd"></td></tr><tr><td>有效期:</td><td><input type="radio" name="timeout" value="-1" checked="checked">關(guān)閉瀏覽器即失效<br/><input type="radio" name="timeout" value="<%=30*24*60*60 %>">30天內(nèi)有效<br/><input type="radio" name="timeout" value="<%=Integer.MAX_VALUE %>">永久有效</td></tr> <tr><td colspan="2" align="center"><input type="submit" value="登 錄" ></td></tr> </table></form></fieldset><%} %></body> </html>ForeverLoginServlet類,判斷調(diào)用用戶登錄方法或用戶注銷的方法
import java.io.IOException; import java.io.PrintWriter; 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;import com.cn.zj.tool.MakeMD5;public class ForeverLoginServlet extends HttpServlet {/*** Constructor of the object.*/public ForeverLoginServlet() {super();}/*** Destruction of the servlet. <br>*/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 {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8"); //設(shè)置請(qǐng)求編碼格式response.setCharacterEncoding("UTF-8"); //設(shè)置響應(yīng)編碼格式String action = request.getParameter("action");//獲得action參數(shù),主要判斷是登錄還是注銷if("login".equals(action)){this.login(request, response); //調(diào)用login方法}else if("logout".equals(action)){this.logout(request, response); //調(diào)用logout方法}}/*** 該方法處理用戶登錄* @param request* @param response* @throws ServletException* @throws IOException*/public void login(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{String account = request.getParameter("account"); //獲得賬號(hào)String pwd = request.getParameter("pwd"); //獲得密碼int timeout= Integer.parseInt(request.getParameter("timeout"));//獲得登錄保存時(shí)間的期限String md5Account = MakeMD5.getMD5(account); //將賬號(hào)加密account = URLEncoder.encode(account,"UTF-8"); //如果賬號(hào)是中文,需要轉(zhuǎn)換Unicode才能保存在Cookie中Cookie accountCookie = new Cookie("account",account);//將賬號(hào)保存在Cookie中accountCookie.setMaxAge(timeout); //設(shè)置賬號(hào)Cookie的最大保存時(shí)間Cookie md5AccountCookie = new Cookie("md5Account",md5Account);//將加密后的賬號(hào)保存在Cookie中md5AccountCookie.setMaxAge(timeout); //設(shè)置加密后的賬號(hào)最大保存時(shí)間response.addCookie(accountCookie); //寫到客戶端的Cookie中response.addCookie(md5AccountCookie); //寫到客戶端的Cookie中try {Thread.sleep(1000); //將此線程暫停1秒后繼續(xù)執(zhí)行} catch (InterruptedException e) { e.printStackTrace();}//將頁面重定向到用戶登錄頁response.sendRedirect("index.jsp?"+System.currentTimeMillis());}/*** 該方法處理用戶注銷* @param request* @param response* @throws ServletException* @throws IOException*/public void logout(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{Cookie accountCookie = new Cookie("account",""); //創(chuàng)建一個(gè)空的CookieaccountCookie.setMaxAge(0); //設(shè)置此Cookie保存時(shí)間為0Cookie md5AccountCookie = new Cookie("md5Account","");//創(chuàng)建一個(gè)空的Cookiemd5AccountCookie.setMaxAge(0); //設(shè)置此Cookie保存時(shí)間為0response.addCookie(accountCookie); //寫到客戶端Cookie中,將覆蓋名為account的Cookieresponse.addCookie(md5AccountCookie); //寫到客戶端Cookie中,將覆蓋名為md5AccountCookie的Cookietry {Thread.sleep(1000); //將此線程暫停1秒后繼續(xù)執(zhí)行} catch (InterruptedException e) { e.printStackTrace();}//將頁面重定向到用戶登錄頁response.sendRedirect("index.jsp?"+System.currentTimeMillis());}public void init() throws ServletException {}}web.xml文件配置
<servlet><servlet-name>ForeverLoginServlet</servlet-name><servlet-class>com.cn.zj.Servlet.ForeverLoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>ForeverLoginServlet</servlet-name><url-pattern>/foreverlogin</url-pattern></servlet-mapping>總結(jié)
以上是生活随笔為你收集整理的利用Servlet实现用户永久登录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servlet实现的个人所得税计算器
- 下一篇: 创建过滤器