Case: Remember the last visit time
1. Requirements:
① To visit a Servlet, if it is the first visit, it will prompt: Hello, welcome to visit for the first time.
②If it is not the first time to visit, it will prompt: Come back with Huanyi, the time of your last visit is: display time string
2. Analysis:
①It can be done with Cookie
②The Servlet in the server judges whether there is a cookie named lastTime
ⅠYes: Not the first visit
* Response data: Welcome back, your last visit time was: 08:05:20, November 1, 2019
* Write back Cookie: lastTmie=08:05:10, November 1, 2019
Ⅱ No: this is the first visit
* Response data: Hello, welcome to visit for the first time
* Write back the Cookie: lastTmie=08:05:10, November 1, 2019
package jason.lh.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.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author: Jason
* @Date: 2019/11/1 7:50
* @Version 1.0
*/
@WebServlet("/CookieTest")
public class CookieTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应的消息体的数据格式及编码
response.setContentType("text/html;charset=utf-8");
//①获取所有的Cookie
Cookie[] cookies = request.getCookies();
boolean flag = false; //没有cookie为lastTime
//②遍历Cookie数组
if ((cookies != null) & (cookies.length > 0)){
for (Cookie cookie : cookies) {
//③获取cookie的名称
String name = cookie.getName();
//④判断名称是否是:lastTime
if ("lastTime".equals(name)){
flag = true; //找到lastTime的cookie为true
//有该cookie,不是第一次访问
//设置cookie的value值
//获取当前时间的字符串,重新设置cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date); //解析时间格式
cookie.setValue(str_date);
//设置cookie的存活时间;
cookie.setMaxAge(60*60*24*30); //一个月
response.addCookie(cookie);
//响应数据
//获取cookie的value值
String value = cookie.getValue();
response.getWriter().write("<h1>欢迎回来,你上次访问时间为:" + value + "</h1>");
break;
}
}
}
if (cookies == null || cookies.length == 0 || flag == false ){
//没有lastTime 表示第一次访问
//设置cookie的value值
//获取当前时间的字符串,重新设置cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date); //解析时间格式
Cookie cookie = new Cookie("lastTime",str_date);
cookie.setValue(str_date);
//设置cookie的存活时间;
cookie.setMaxAge(60*60*24*30); //一个月
response.addCookie(cookie);
response.getWriter().write("<h1>您好,欢迎您是首次访问 </h1>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
Running result: 500 error
Found the wrong place:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
There is a space between: after tomcat 8, cookies support Chinese data.
However, special characters are still not supported, and URL encoding must be used for storage and URL encoding analysis.
After modification:
package jason.lh.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;
/**
* @Author: Jason
* @Date: 2019/11/1 7:50
* @Version 1.0
*/
@WebServlet("/CookieTest")
public class CookieTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应的消息体的数据格式及编码
response.setContentType("text/html;charset=utf-8");
//①获取所有的Cookie
Cookie[] cookies = request.getCookies();
boolean flag = false; //没有cookie为lastTime
//②遍历Cookie数组
if ((cookies != null) & (cookies.length > 0)) {
for (Cookie cookie : cookies) {
//③获取cookie的名称
String name = cookie.getName();
//④判断名称是否是:lastTime
if ("lastTime".equals(name)) {
flag = true; //找到lastTime的cookie为true
//有该cookie,不是第一次访问
//先获取前一次访问的cookie的value值
//响应数据
//获取cookie的value值,时间
String value = cookie.getValue();
System.out.println("解码前:" + value);
//URL解码
value = URLDecoder.decode(value, "utf-8");
System.out.println("解码后:" + value);
response.getWriter().write("<h1>欢迎回来,你上次访问时间为:" + value + "</h1>");
//再设置cookie的value值
//获取当前时间的字符串,重新设置cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date); //解析时间格式
System.out.println("编码前:" + str_date);
//URL编码
str_date = URLEncoder.encode(str_date, "utf-8");
System.out.println("编码后:" + str_date);
cookie.setValue(str_date);
//设置cookie的存活时间;
cookie.setMaxAge(60 * 60 * 24 * 30); //一个月
response.addCookie(cookie);
/*//响应数据
//获取cookie的value值,时间
String value = cookie.getValue();
System.out.println("解码前:" + value);
//URL解码
value = URLDecoder.decode(value, "utf-8");
System.out.println("解码后:" + value);
response.getWriter().write("<h1>欢迎回来,你上次访问时间为:" + value + "</h1>");
*/
break;
}
}
}
if (cookies == null || cookies.length == 0 || flag == false) {
//没有lastTime 表示第一次访问
//设置cookie的value值
//获取当前时间的字符串,重新设置cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date); //解析时间格式
System.out.println("编码前:" + str_date);
//URL编码
str_date = URLEncoder.encode(str_date, "utf-8");
System.out.println("编码后:" + str_date);
Cookie cookie = new Cookie("lastTime", str_date);
cookie.setValue(str_date);
//设置cookie的存活时间;
cookie.setMaxAge(60 * 60 * 24 * 30); //一个月
response.addCookie(cookie);
response.getWriter().write("<h1>您好,欢迎您是首次访问 </h1>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
The first time I forgot to take a screenshot