一. 需求
访问一个 Servlet,如果是第一次访问,提示:您好,欢迎您首次访问;否则,提示:欢迎回来,您上次访问的时间为:(显示时间字符串)
二. 实现
- 使用 Servlet
package com.hjplz.cookie;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Cookie Demo: 记住上一次的访问时间
*/
@WebServlet("/cookieTest")
public class CookieTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置 response 的数据&编码格式
response.setContentType("text/html;charset=UTF-8");
// 1. 获取所有 Cookie
Cookie[] cookies = request.getCookies();
boolean flag = false; // 默认 Cookie 中无 lastTime
// 2. 遍历 Cookie 数组,查找 lastTime
if(cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
// 有该 Cookie,不是第一次访问
if("lastTime".equals(cookie.getName())) {
flag = true;
// 1. 响应数据:欢迎回来,您上次访问时间为:lastTime
String value = cookie.getValue();
value = URLDecoder.decode(value, "UTF-8"); // 对时间字符串进行 URL 解码
response.getWriter().write("欢迎回来,您上次访问时间为:" + value);
// 2. 获取时间字符串并进行 URL 编码
String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
date = URLEncoder.encode(date, "UTF-8");
// 3. 重新设置 Cookie 值
cookie.setValue(date);
// 4. 设置 Cookie 的存活时间
cookie.setMaxAge(60 * 60 * 24 * 30); // 1 month
// 5. 重新发送 Cookie
response.addCookie(cookie);
break;
}
}
}
// 无该 Cookie,是第一次访问
if((cookies == null) || (cookies.length == 0) || !flag) {
// 1. 响应数据:您好,欢迎首次访问!
response.getWriter().write("您好,欢迎首次访问!");
// 2. 获取时间字符串并进行 URL 编码
String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
date = URLEncoder.encode(date, "UTF-8");
// 3. 创建 Cookie : lastTime=date
Cookie cookie = new Cookie("lastTime", date);
// 4. 设置 Cookie 的存活时间
cookie.setMaxAge(60 * 60 * 24 * 30); // 1 month
// 5. 重新发送 Cookie
response.addCookie(cookie);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
- 使用 JSP
<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>showTime</title>
</head>
<body>
<%
// 1. 获取所有 Cookie
Cookie[] cookies = request.getCookies();
boolean flag = false; // 默认 Cookie 中无 lastTime
// 2. 遍历 Cookie 数组,查找 lastTime
if(cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
// 有该 Cookie,不是第一次访问
if("lastTime".equals(cookie.getName())) {
flag = true;
// 1. 响应数据:欢迎回来,您上次访问时间为:lastTime
String value = cookie.getValue();
value = URLDecoder.decode(value, "UTF-8"); // 对时间字符串进行 URL 解码
%>
<h1>欢迎回来,您上次访问时间为:<%= value %></h1>
<%
// 2. 获取时间字符串并进行 URL 编码
String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
date = URLEncoder.encode(date, "UTF-8");
// 3. 重新设置 Cookie 值
cookie.setValue(date);
// 4. 设置 Cookie 的存活时间
cookie.setMaxAge(60 * 60 * 24 * 30); // 1 month
// 5. 重新发送 Cookie
response.addCookie(cookie);
break;
}
}
}
// 无该 Cookie,是第一次访问
if((cookies == null) || (cookies.length == 0) || !flag) {
// 1. 响应数据:您好,欢迎首次访问!
%>
<h1>您好,欢迎首次访问!</h1>
<%
// 2. 获取时间字符串并进行 URL 编码
String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
date = URLEncoder.encode(date, "UTF-8");
// 3. 创建 Cookie : lastTime=date
Cookie cookie = new Cookie("lastTime", date);
// 4. 设置 Cookie 的存活时间
cookie.setMaxAge(60 * 60 * 24 * 30); // 1 month
// 5. 重新发送 Cookie
response.addCookie(cookie);
}
%>
</body>
</html>
三. 效果演示
-
首次访问
-
再次访问