访问路径:http://localhost:8080/day18/home.jsp
home.jsp
<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上次访问时间</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
boolean flag = false;
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
if ("lastTime".equals(name)) {
flag = true;
String value = cookie.getValue();
System.out.println("解码前: " + value);
value = URLDecoder.decode(value, "utf-8");
System.out.println("解码后:" + value);
%>
<h1>欢迎回来...上次访问时间为:<%=value%>></h1>
<input><input>
<%
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);
str_date = URLEncoder.encode(str_date, "utf-8");
System.out.println("编码后: " + str_date);
cookie.setValue(str_date);
cookie.setMaxAge(60 * 60 * 24 * 30);
response.addCookie(cookie);
break;
}
}
}
if (cookies == null || cookies.length == 0 || flag == false) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date);
System.out.println("编码前1: " + str_date);
str_date = URLEncoder.encode(str_date, "utf-8");
System.out.println("编码后1: " + str_date);
Cookie cookie = new Cookie("lastTime", str_date);
response.addCookie(cookie);
%>
<h1>你好,欢迎首次访问...</h1>
<% } %>
</body>
</html>
|