cookie实现上次访问时间和清除上次访问时间
来源:互联网 发布:经济型数控车床编程 编辑:程序博客网 时间:2024/05/06 01:12
package cookie;import java.io.IOException;import java.io.PrintWriter;import java.sql.Date;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//网站首页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.print("<a href='/Cookie/servlet/CookieDemo2'>清楚上次访问时间</a><br/>"); out.print("您上次访问时间是: "); //获得用户的时间cookie Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null && 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(1*30*24*3600); //设置有效路径 cookie.setPath("/Cookie"); response.addCookie(cookie); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
package cookie;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class CookieDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie = new Cookie("lastAccessTime",System.currentTimeMillis()+""); cookie.setMaxAge(0); cookie.setPath("/Cookie"); response.addCookie(cookie); //response.sendRedirect("/Cookie/servlet/CookieDemo1"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
第一次访问
第二次访问
点击清除上次访问记录
重新访问
0 0
- cookie实现上次访问时间和清除上次访问时间
- cookie实现上次访问时间
- 利用cookie实现用户上次访问时间
- Cookie纪录上次访问时间
- cookie显示上次访问时间
- cookie记录上次访问时间
- 利用cookie获得上次访问时间
- Cookie案例:显示上次访问的时间
- cookie--显示用户上次访问时间
- cookie案例,显示用户上次访问时间
- Cookie应用:显示上次访问页面时间
- Cookie,session记录上次访问时间
- 获得上次访问时间
- 记录上次访问时间
- cookie的介绍,实现显示用户上次访问时间
- 记录用户上次访问时间(cookie和session)
- cookie在web应用中实现上次登录时间以及上次访问时间 小案例理解cookie
- 记录您上次访问时间
- sprintf函数的再封装
- a special date
- Android Service简介
- 用const定义的常量比用#define定义的常量有那些优势
- CBLAS的安装与使用
- cookie实现上次访问时间和清除上次访问时间
- Create A Simple RESTful API With GoLang
- java反射机制
- 行主序 vs 列主序 Row Major vs Column Major Vector
- C(3)
- μC/OS任务状态及具体内容描述
- 分布式原理与实战(三) 以FDB为例之通信
- 论文笔记 You Only Look Once: Unified, Real-Time Object Detection
- 串口中怎样接收一个完整数据包的解析