******************************************************************************************
public class CookieDemo1 extends HttpServlet {
public 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.write("您上次的访问时间是:");
// 取到指定的Cookie lastAccessTime的值,打印到页面上
Cookie cs[] = request.getCookies();
for (int i = 0; cs != null && i < cs.length; i++) {
Cookie c = cs[i];
if ("lastAccessTime".equals(c.getName())) {
String value = c.getValue();
long time = Long.parseLong(value);
out.print(new Date(time).toLocaleString());
}
}
// 把当前的访问时间写回去
Cookie c = new Cookie("lastAccessTime", System.currentTimeMillis() + "");
// 设置存储时间 如果不设置,则只存储在内存中,存储时间为一个会话的时间
// c.setMaxAge(Integer.MAX_VALUE); // 永久存储
c.setMaxAge(30 * 24 * 60 * 60); // 时间为30天
// 设置当前cookie的路径 request.getContextPath() : 获得当前应用的名称 即/day07
c.setPath(request.getContextPath());
response.addCookie(c);
// 清空存储时间
out.write("<br/>" + "<a href='/day07/servlet/CookieDemo2' >clear</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
-----------------------------------------------------------------------------------------------------
// 清空指定的Cookie
public class CookieDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 清空cookie
Cookie c = new Cookie("lastAccessTime","");
c.setMaxAge(0);
// 将cookie写回去,注意保持当前cookie的路径一置
c.setPath(request.getContextPath());
response.addCookie(c);
// 刷新到原来的页面
response.setHeader("Refresh", "3;URL=/day07/servlet/CookieDemo1");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
******************************************************************************************
public class CookieDemo1 extends HttpServlet {
public 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.write("您上次的访问时间是:");
// 取到指定的Cookie lastAccessTime的值,打印到页面上
Cookie cs[] = request.getCookies();
for (int i = 0; cs != null && i < cs.length; i++) {
Cookie c = cs[i];
if ("lastAccessTime".equals(c.getName())) {
String value = c.getValue();
long time = Long.parseLong(value);
out.print(new Date(time).toLocaleString());
}
}
// 把当前的访问时间写回去
Cookie c = new Cookie("lastAccessTime", System.currentTimeMillis() + "");
// 设置存储时间 如果不设置,则只存储在内存中,存储时间为一个会话的时间
// c.setMaxAge(Integer.MAX_VALUE); // 永久存储
c.setMaxAge(30 * 24 * 60 * 60); // 时间为30天
// 设置当前cookie的路径 request.getContextPath() : 获得当前应用的名称 即/day07
c.setPath(request.getContextPath());
response.addCookie(c);
// 清空存储时间
out.write("<br/>" + "<a href='/day07/servlet/CookieDemo2' >clear</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
-----------------------------------------------------------------------------------------------------
// 清空指定的Cookie
public class CookieDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 清空cookie
Cookie c = new Cookie("lastAccessTime","");
c.setMaxAge(0);
// 将cookie写回去,注意保持当前cookie的路径一置
c.setPath(request.getContextPath());
response.addCookie(c);
// 刷新到原来的页面
response.setHeader("Refresh", "3;URL=/day07/servlet/CookieDemo1");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
******************************************************************************************