Cookie小案例: 1记住用户最后一次的访问时间 2记住用户登录时的用户名 // 1利用Cookie技术,记住用户上次访问的时间 public class ShowLastAccessTimeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;character=utf-8"); PrintWriter out = response.getWriter(); out.write("您上次访问的时间是:"); // 显示上次记录的时间 Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { Cookie cookie = cookies[i]; if ("lastAccessTime".equals(cookie.getName())) { String value = cookie.getName(); long time = Long.parseLong(value); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); out.write(dateFormat.format(time)); // out.write(new Date(time).toLocaleString()); // 过时了 } } // 向客户端写一个cookie,取当前的访问时间为它的值 Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis() + ""); // 设置maxAge cookie.setMaxAge(Integer.MAX_VALUE); cookie.setPath(request.getContextPath()); response.addCookie(cookie); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ---------------------------------------------------------- // 2.1展示登录界面,表单自动读取指定的cookie public class ShowLoginFormServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;character=utf-8"); PrintWriter out = response.getWriter(); // 读取指定的cookie,准备回显的数据 String username = ""; String checked = ""; Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { Cookie cookie = cookies[i]; if ("username".equals(cookie.getName())) { username = cookie.getName(); checked = "checked='checked'"; break; } } // 展示登录界面 out.write("<form action='"+ request.getContextPath() + "/RememberUserServlet' method='post' >"); out.write("username:<input type='text' name='username' value='" + username + "' /><br/>"); out.write("password:<input type='password' name='password' value='' /><br/>"); out.write("remember:<input type='checkbox' name='remember' value='" + checked + "' /><br/>"); out.write("<input type='submit' /><br/>"); out.write("</form>"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } // 2.2完成登录,并且向浏览器写入cookie public class RememberUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;character=utf-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String remember = request.getParameter("remember"); Cookie cookie = new Cookie("username", username); cookie.setPath(request.getContextPath()); if(remember != null){ cookie.setMaxAge(Integer.MAX_VALUE); }else { cookie.setMaxAge(0); } response.addCookie(cookie); out.write("登录成功!"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ---------------------------------------------------------- 补充: 购物车的信息在关闭了浏览器后,再重新打开浏览器时还依然存在的解决方法: //自己写名为"JSESSIONID"的Cookie,覆盖掉原来的 Cookie c = new Cookie("JSESSIONID",session.getId()); c.setPath(request.getContextPath()); c.setMaxAge(Integer.MAX_VALUE); response.addCookie(c);
时间: 2024-10-27 08:52:46