public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Cookie cookies[] = request.getCookies(); for(int i = 0; cookies != null && i < cookies.length; i++) { if(cookies[i].getName().equals("lasttime")) { Long t = Long.parseLong(cookies[i].getValue()); Date d = new Date(t); out.println(d); break; } } Cookie cook = new Cookie("lasttime", System.currentTimeMillis()+""); cook.setMaxAge(24*3600);//保存多少秒 cook.setPath("/Learn");//设置cookie的有效路径 默认是当前servlet目录 response.addCookie(cook); }
setMaxAge 不设置是会话级别 进程结束就消失
时间: 2024-10-16 18:27:35