记录上传登录时间![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/88f3454b2096d01165fd26943e4223c7.png)
package com.itheima.lastaccesstime;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LastAccessTimeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String currentTime = format.format(date);
Cookie cookie = new Cookie("lastAccessTime",currentTime);
cookie.setMaxAge(60*10*500);
response.addCookie(cookie);
String lastAccessTime = null;
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie coo : cookies){
if("lastAccessTime".equals(coo.getName())){
lastAccessTime = coo.getValue();
}
}
}
response.setContentType("text/html;charset=UTF-8");
if(lastAccessTime==null){
response.getWriter().write("您是第一次访问");
}else{
response.getWriter().write("您上次的访问的时间是:"+lastAccessTime);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}