java中对cookie的简单处理

来源:互联网 发布:sql修改字段名称和类型 编辑:程序博客网 时间:2024/04/30 11:52
  1. class  
  2. {
  3.     public ActionForward index(ActionMapping mapping, ActionForm form,
  4.             HttpServletRequest request, HttpServletResponse response) {
  5.         
  6.         Cookie[] cookies = request.getCookies();
  7.         if (null == request.getSession().getAttribute("userMsg")) {
  8.             if (cookies != null) {
  9.                 boolean cookie = false;
  10.                 String userName = "";
  11.                 for (int i = 0; i < cookies.length; i++) {
  12.                     if (cookies[i].getName().equals("boyin")) {
  13.                         userName = cookies[i].getValue();
  14.                         cookie = true;
  15.                         break;
  16.                     }
  17.                 }
  18.                 if (cookie = true) {
  19.                     
  20.                     //业务处理
  21.                 }
  22.             }
  23.         }
  24.         return mapping.findForward("builderIndex");
  25.     
  26.     }
  27. }
  28. //写cookie
  29. Cookie newcookie = new Cookie("name", userName1);
  30. newcookie.setDomain("boyincn.com");
  31. newcookie.setMaxAge(60 * 60);
  32. response.addCookie(newcookie);