public class HistServlet2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String curTime = sdf.format(new Date());
Cookie[] cookies = request.getCookies();
String lastTime = null;
if (cookies!=null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("lastTime")) {
lastTime=cookie.getValue();
response.getWriter().write("您上次访问的时间是:"+lastTime+"现在的时间是"+curTime);
cookie.setValue(curTime);
response.addCookie(cookie);
}
}
}
if (cookies==null||lastTime==null) {
response.getWriter().write("您是第一次访问本网站,当前时间是"+curTime);
Cookie cookie = new Cookie("lastTime", curTime);
cookie.setMaxAge(1*60*24);
response.addCookie(cookie);
}
}
}
案例-用户上次访问时间
最新推荐文章于 2022-08-25 01:00:00 发布