| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

Cookie操作Java工具类

import javax.servlet.http.Cookie;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;        /**   * @author HuifengWang 操作cookie的工具类   *    */  public class CookieUtil {      /**       * 根据cookie的名称获取cookie       * @param request       * @param name       * @return       */      public static Cookie getCookie(HttpServletRequest request, String name) {          Cookie cookies[] = request.getCookies();          if (cookies == null || name == null || name.length() == 0) {              return null;          }          for (int i = 0; i < cookies.length; i++) {              if (name.equals(cookies[i].getName())){                      //&& request.getServerName().equals(cookies[i].getDomain())) {                  return cookies[i];              }          }          return null;      }         public static String getCookieValue(HttpServletRequest request, String name){          Cookie ck = getCookie(request, name);          if(ck!=null){              return PwdCrypt.decrypt(ck.getValue());          }else{              return null;          }      }             /**       * 删除cookie       * @param request       * @param response       * @param cookie       */      public static void deleteCookie(HttpServletRequest request,              HttpServletResponse response, Cookie cookie) {          if (cookie != null) {              cookie.setPath(getPath(request));              cookie.setValue("");              cookie.setMaxAge(0);              response.addCookie(cookie);          }      }         /**       * 设置cookie       * @param request       * @param response       * @param name       * @param value       * 如果不设置时间,默认永久       */      public static void setCookie(HttpServletRequest request,              HttpServletResponse response, String name, String value) {          setCookie(request, response, name, value, 0x278d00);      }         /**       * @param request       * @param response       * @param name       * @param value       * @param maxAge       * 设置cookie,设定时间       */      public static void setCookie(HttpServletRequest request,              HttpServletResponse response, String name, String value, int maxAge) {          String cookieValue = PwdCrypt.encrypt(value);          Cookie cookie = new Cookie(name, value == null ? "" : cookieValue.replaceAll("\r\n", ""));          cookie.setMaxAge(maxAge);          cookie.setPath(getPath(request));          response.addCookie(cookie);      }         private static String getPath(HttpServletRequest request) {          String path = request.getContextPath();          return (path == null || path.length() == 0) ? "/" : path;      }      public static void main(String[] args) {             }  }

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1408436561381.html
Java工具类 Java开发