在网页上使用的都是http协议,三次握手,四次挥手导致每次只能访问一次,不能一直连接在数据库上,于是就有了会话跟踪技术:
一个用户在一定时间类,访问网站相同,那么就不需要再吃验证数据,只需要在第一次访问时给定一个用于辨别用户的ID:
String id = session.getId();
out.print(id);
利用session给定一个ID,它的默认销毁时间时半小时和浏览器关闭,这样在下一次访问时,就不需要再判断了。相当于在登录一个网站时,我们跳转到它的其他网页,就不再需要再次登录账户。
Cookie:
Cookie时存储在浏览器中的,用于记录一些数据,比如记住密码。
它是成对存在,定义Cookie名字和CookieValue还有销毁时间;
这里我们做一个简单的记住密码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<style type="text/css">
#p {
display: none;
}
</style>
<body>
<%
String s = "" + request.getAttribute("cw");
String name = "";
String pwd = "";
Cookie[] cookie = request.getCookies();
for (Cookie ck : cookie) {
if (ck != null) {
String t = ck.getName();
if (t.equals("name")) {
name = ck.getValue();
} else if (t.equals("pwd")) {
pwd = ck.getValue();
}
}
}
%>
<form action="login" method="POST">
用户名:<input type="text" name="name" value=<%=name%>> <br>
密码:<input type="password" name="pwd" value=<%=pwd%>> <br>
记住密码30天<input type="radio" name="radio" value="30" /> <br> <input type="submit" value="提交">
</form>
<p id="p"><%=s%></p>
<script type="text/javascript">
var name = document.getElementById("p").innerHTML;
if (name != "null") {
alert(name);
}
</script>
</body>
</html>
选中记住密码时,在后台判断密码正确后,就会创建两个Cookie,记录用户名和密码,下一次再登录这个网页时,会自动填上。
Servlet代码:
package Servlet;
import java.io.IOException;
import Dao.loginDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet implementation class loginServlet
*/
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public loginServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String RadioCookie = request.getParameter("radio");
loginDao dao = new loginDao();
if (dao.check(name, pwd)) {
if (RadioCookie != null) {
Cookie ckName = new Cookie("name", name);
Cookie ckPwd = new Cookie("pwd", pwd);
ckPwd.setMaxAge(60 * 60 * 24 * 30);//销毁时间
ckName.setMaxAge(60 * 60 * 24 * 30);
response.addCookie(ckPwd);
response.addCookie(ckName);
}
request.getRequestDispatcher("showId.jsp").forward(request, response);
} else {
// 密码错误
request.setAttribute("cw", "账号或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}