jsp使用cookie實現記住用戶名和密碼


首先說一下實現的功能:
用戶打開注冊頁面,最下面有個記住用戶名和密碼的復選框,如果勾選上,則在登錄頁面會自動將用戶名和密碼賦值到文本框中,使用java中的cookie實現,下面就是代碼:
注冊頁面代碼(reg.jsp):

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'reg.jsp' starting page</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">
	-->

  </head>
  
  <body>
     <h1>注冊頁面</h1>
     <form action="doreg.jsp" method="post">
     	用戶名:<input type="text" name="name"/><br/>
     	密碼:<input type="text" name="pass"/><br/>
     	<input type="checkbox" name="jizhu"/>記住用戶名和密碼
     	<br/>     	
     	<input type="submit" value="注冊"/>
     	<input type="reset" value="重置"/>
     </form>
  </body>
</html>

運行結果如圖所示:
在這里插入圖片描述
在這里插入圖片描述

點擊注冊的按鈕時,將表單信息提交到doreg.jsp頁面,下面是doreg.jsp頁面的代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	//解決亂碼
	request.setCharacterEncoding("utf-8");
	//獲取記住密碼的框是否選中
	String jizhu = request.getParameter("jizhu");
	if(jizhu!=null){
		//獲取值
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		//將值放在cookie里面
		Cookie c1 = new Cookie("uname",name);
		Cookie c2 = new Cookie("upass",pass);
		response.addCookie(c1);
		response.addCookie(c2);
		//重定向到登陸頁面
		response.sendRedirect("login.jsp");
	}
	
 %>

這個頁面主要是處理業務,所有將jsph中的html代碼都已去掉,全部以小腳本的方式寫的。先判斷注冊時是否勾選記住用戶名和密碼的復選框,如果勾選則將用戶名和密碼放到cookie里,最后重定向到登錄頁面login.jsp里。

下面是login頁面的代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</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">

  </head>
  
  <body>
  <%
  	String name="";
  	String pass="";
  	//獲取cookie里面的值
  	Cookie [] cookies = request.getCookies();
     	 if(cookies!=null){
     	 	//遍歷cookie
     	 	for(int i = 0;i<cookies.length;i++){
     	 		if(cookies[i].getName().equals("uname")){
     	 			//獲取cookie里面的用戶名
     	 			name = cookies[i].getValue();
     	 		}else if(cookies[i].getName().equals("upass")){
     	 			//獲取密碼
     	 			pass = cookies[i].getValue();
     	 		}
     	 	}
     	 }
   %>
     <h1>登錄頁面</h1>
       <form action="dologin.jsp" method="post">
     	用戶名:<input type="text" name="name" value="<%=name%>"/><br/>
     	密碼:<input type="text" name="pass" value="<%=pass%>"/><br/>
     	<input type="submit" value="登錄"/>
     	<input type="reset" value="重置"/>
     </form>
  </body>
</html>

運行截圖如下所示:
在這里插入圖片描述
其中,Cookie的getName是獲取存放的鍵,getValue獲取的是值。
歡迎留言評論,公眾號:雄雄的小課堂。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM