protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置防止乱码
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("您上次访问的时间是:");
//拿cookie
Cookie[] cookies = request.getCookies();
for(int i=0; cookies!=null&&i<cookies.length;i++){
if(cookies[i].getName().equals("lastAccessTime")){
//得到时间并转成毫秒
Long cookieValue = Long.parseLong(cookies[i].getValue());
//
Date date = new Date(cookieValue);
out.print(date.toLocaleString());
}
}
//给用户送cookie
Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+"");
cookie.setMaxAge(3600*24);
response.addCookie(cookie);
}
用浏览器访问此servlet2次可得时间
servlet显示上次用户访问的时间
最新推荐文章于 2021-11-15 15:59:00 发布