Example: Remember the last access time 1. Requirements: 1. Access a Servlet. 2. If it is not the first time, the system displays a message: Welcome back. The last time you accessed the system is: Displays the time string
The Servlet in the server determines whether there is a Cookie named lastTime. 1. There are: not first access 1. Response data: Welcome back, you last accessed: June 10, 2018 11:50:20 2. Write back to Cookie: lastTime= jun 10, 2018 11:50:01 2. 1. Response data: Hello, welcome to your first visit 2. Write back to Cookie: lastTime= June 10, 2018 11:50:01Copy the code
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;
@WebServlet("/cookieTest")
public class CookieTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set the data format and encoding of the message body of the response
response.setContentType("text/html; charset=utf-8");
//1. Get all cookies
Cookie[] cookies = request.getCookies();
boolean flag = false;// No cookie is lastTime
//2. Iterate over the cookie array
if(cookies ! =null && cookies.length > 0) {for (Cookie cookie : cookies) {
//3. Get the name of the cookie
String name = cookie.getName();
//4. Check whether the name is lastTime
if("lastTime".equals(name)){
// There is this Cookie, not the first access
flag = true;// There is a lastTime cookie
// Set the value of the Cookie
// Get the current time string, reset the value of the Cookie, resend the Cookie
Date date = new Date();
// Convert the format
SimpleDateFormat sdf = new SimpleDateFormat("Yyyy yyyy MM dd day HH: MM :ss");
String str_date = sdf.format(date);
cookie.setValue(str_date);
// Sets the lifetime of the cookie
cookie.setMaxAge(60 * 60 * 24 * 30);/ / for a month
response.addCookie(cookie);// Resend the cookie
// Response data
// Get the Cookie value, time
String value = cookie.getValue();
response.getWriter().write(" Welcome back, you last visited at :"
+value+"</h1>");
break; }}}if(cookies == null || cookies.length == 0 || flag == false) {// No, first visit
// Set the value of the Cookie
// Get the current time string, reset the value of the Cookie, resend the Cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("Yyyy yyyy MM dd day HH: MM :ss");
String str_date = sdf.format(date);
Cookie cookie = new Cookie("lastTime",str_date);
// Sets the lifetime of the cookie
cookie.setMaxAge(60 * 60 * 24 * 30);/ / for a month
response.addCookie(cookie);
response.getWriter().write("< H1 > Hello, welcome to your first visit to "); }}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response); }}Copy the code
Character [32] ASCII Chinese space “YYYY year MM month DD day HH: MM :ss” contains Spaces
Before Tomcat 8, Chinese data could not be stored directly in cookies. Chinese data needs to be transcoded - generally using URL encoding (%E3) after Tomcat 8, cookies support Chinese data. Str_date = urlencoder. encode(str_date, "UTF-8"); str_date = URLEncoder. Encoding. Use value = urldecoder.decode (value, "UTF-8"); Decoding.Copy the code
— — — — — — — —
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;
@WebServlet("/cookieTest")
public class CookieTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set the data format and encoding of the message body of the response
response.setContentType("text/html; charset=utf-8");
//1. Get all cookies
Cookie[] cookies = request.getCookies();
boolean flag = false;// No cookie is lastTime
//2. Iterate over the cookie array
if(cookies ! =null && cookies.length > 0) {for (Cookie cookie : cookies) {
//3. Get the name of the cookie
String name = cookie.getName();
//4. Check whether the name is lastTime
if("lastTime".equals(name)){
// There is this Cookie, not the first access
flag = true;// There is a lastTime cookie
// Set the value of the Cookie
// Get the current time string, reset the value of the Cookie, resend the Cookie
Date date = new Date();
// Convert the format
SimpleDateFormat sdf = new SimpleDateFormat("Yyyy yyyy MM dd day HH: MM :ss");
String str_date = sdf.format(date);
System.out.println("Before coding:"+str_date);
/ / URL encoding
str_date = URLEncoder.encode(str_date,"utf-8");
System.out.println("After coding:"+str_date);
cookie.setValue(str_date);
// Sets the lifetime of the cookie
cookie.setMaxAge(60 * 60 * 24 * 30);/ / for a month
response.addCookie(cookie);// Resend the cookie
// Response data
// Get the Cookie value, time
String value = cookie.getValue();
System.out.println("Before decoding:"+value);
/ / URL decoding:
value = URLDecoder.decode(value,"utf-8");
System.out.println("After decoding:"+value);
response.getWriter().write(" Welcome back, you last visited at :"
+value+"</h1>");
break; }}}if(cookies == null || cookies.length == 0 || flag == false) {// No, first visit
// Set the value of the Cookie
// Get the current time string, reset the value of the Cookie, resend the Cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("Yyyy yyyy MM dd day HH: MM :ss");
String str_date = sdf.format(date);
System.out.println("Before coding:"+str_date);
/ / URL encoding
str_date = URLEncoder.encode(str_date,"utf-8");
System.out.println("After coding:"+str_date);
Cookie cookie = new Cookie("lastTime",str_date);
// Sets the lifetime of the cookie
cookie.setMaxAge(60 * 60 * 24 * 30);/ / for a month
response.addCookie(cookie);
response.getWriter().write("< H1 > Hello, welcome to your first visit to "); }}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response); }}Copy the code