cookie使用方法

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();
out.print("上传访问时间:");

Cookie cookies[] = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (int i = 0; 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 = new Cookie("lastAccessTime", System.currentTimeMillis()+"");
cookie.setMaxAge(10); //以秒为单位(设置cookie的生命周期)
cookie.setPath("/JavaWebProject");
response.addCookie(cookie);

你可能感兴趣的:(cookie)