Cookie學習總結-登陸案例(記住使用者名稱和密碼)
阿新 • • 發佈:2019-02-08
LoginServlet.java
package blank.servlet;
import java.io.IOException;
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 LoginServlet extends HttpServlet {
// 宣告cookie物件
private Cookie cookieName = null;
private Cookie cookiePass = null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//建立cookie物件
createCookies(request);
// 獲取oper引數的值
String oper = request.getParameter("oper");
// 判斷引數值
if ("pre".equals(oper)) {
//獲取cookie中的值
String name = cookieName.getValue();
String pass = cookiePass.getValue();
//判斷是否是初始化的值
if(!"1".equals(name)){
//儲存到request域中
request.setAttribute("name", name);
}
//判斷是否是初始化的值
if(!"1".equals(pass)){
//儲存到request域中
request.setAttribute("pass", pass);
}
// 跳轉到登陸介面
request.getRequestDispatcher("/login.jsp").forward(request,
response);
} else if ("login".equals(oper)) {
// 登陸處理的時候
//獲取使用者名稱
String name = request.getParameter("name");
//獲取使用者的密碼
String pass = request.getParameter("pass");
if ("leo".equals(name) && "leo".equals(pass)) {
//修改cookie資訊的值
cookieName.setValue(name);
cookiePass.setValue(pass);
// 響應給瀏覽器的操作
response.addCookie(cookieName);
response.addCookie(cookiePass);
// 跳轉到登陸介面
request.getRequestDispatcher("./index.jsp").forward(request,
response);
} else {
//重新修改值
cookieName.setValue(name);
//刪除cookiePass
cookiePass.setValue("1");//刪除
response.addCookie(cookiePass);//響應給瀏覽器
response.addCookie(cookieName);
// 跳轉到登陸介面
request.getRequestDispatcher("./login.do?oper=pre").forward(request,
response);
}
}
}
/**
* 建立cookie物件
* @param request
*/
private void createCookies(HttpServletRequest request) {
//獲取所有的cookie物件
Cookie cookies[] = request.getCookies();
if (cookies != null) {
for (Cookie ck : cookies) {
//獲取cookie的名稱
String name = ck.getName();
//判斷
if ("name".equals(name)) {
cookieName = ck;
} else if ("pass".equals(name)) {
cookiePass = ck;
}
}
}
//建立要儲存使用者名稱的cookie物件
if (cookieName == null) {
cookieName = new Cookie("name", "1");
}
//建立要儲存使用者密碼的cookie物件
if (cookiePass == null) {
cookiePass = new Cookie("pass", "1");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
login.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 '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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>使用者登入介面</h3>
<form action="./login.do?oper=login" method="post">
使用者名稱:<input type="text" name="name" value="${name}" /><br /> 密碼:<input type="password"
name="pass" value="${pass}" /><br /> <input type="submit" value="登陸" />
<br/>
</form>
</body>
</html>
index.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 'index.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>
<div>
<a href="./login.do?oper=pre">登陸</a>
</div>
</body>
</html>
2.點選登陸
3.輸入使用者名稱、密碼(正確使用者名稱和密碼都是leo)
測試輸入正確使用者名稱和密碼:
點選登陸跳轉到index.jsp,代表登陸成功。
再次點選登陸,可發現瀏覽器已記住使用者名稱和密碼,直接點登陸即可成功登陸
測試輸入錯誤使用者名稱或密碼:
點選登陸–》登陸失敗,瀏覽器已經記住使用者名稱,並未記錄錯誤密碼