给cookie设置有效期
显示访问时间
public class CookieDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setHeader("Content-type","text/html;charset=utf-8"); Cookie[] cookies = req.getCookies(); PrintWriter out= resp.getWriter(); if (cookies!=null){ out.write("你上一次访问的时间是:"); for (int i = 0; i <cookies.length ; i++) { Cookie cookie = cookies[i]; if (cookie.getName().equals("lastLoginTime")) { Long lastLoginTime = Long.parseLong(cookie.getValue()); Date date = new Date(lastLoginTime); out.write(date.toLocaleString()); } } }else { out.write("这是你第一次访问!"); } Cookie cookie1 = new Cookie("lastLoginTime", System.currentTimeMillis() + ""); cookie1.setMaxAge(24*60*60); resp.addCookie(cookie1); } }
标签:cookies,utf,resp,req,Cookie,讲解,out From: https://www.cnblogs.com/guojianglong/p/17050897.html