标签:pos new ext form 客户端 div let sim false
@WebServlet("/accessTimeServlet") public class AccessTimeServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置 ContentType response.setContentType("text/html;charset=utf-8"); // 获取Cookies Cookie[] cookies = request.getCookies(); boolean flag = false; if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (cookie.getName().equals("lastTime")) { flag = true; // 定义时间 String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); // 通过url解码并输出至窗口 response.getWriter().write("欢迎回来,您上次访问时间为" + URLDecoder.decode(cookie.getValue(), "utf-8")); // 因cookie无法存储空格,所以通过url编码添加 cookie.setValue(URLEncoder.encode(date, "utf-8")); // 设置cookie蠢货时间 cookie.setMaxAge(30 * 24 * 60 * 60); // 添加cookie 值 response.addCookie(cookie); break; } } } if (cookies == null || cookies.length < 1 || flag == false) { response.getWriter().write("您好,欢迎首次访问!"); // 定义时间 String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); // 创建cookie Cookie timeCookie = new Cookie("lastTime", URLEncoder.encode(date, "utf-8")); // 添加进cookies response.addCookie(timeCookie); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
java Servlet中使用Cookie记录浏览器客户端访问时间
标签:pos new ext form 客户端 div let sim false
原文地址:https://www.cnblogs.com/CN-Dragon/p/12996889.html