• cookie工具类


      2 
      3 import javax.servlet.http.Cookie;
      4 import javax.servlet.http.HttpServletRequest;
      5 import javax.servlet.http.HttpServletResponse;
      6 
      7 
      8 public class CookieHelper {
      9     
     10     
     11     /**
     12      * 设置cookie有效期,根据需要自定义[本系统设置为30天]
     13      */
     14     private final static int COOKIE_MAX_AGE = 60 * 60 * 24 * 30;
     15 
     16     /**
     17      * 
     18      * @desc 删除指定Cookie
     19      * @param response
     20      * @param cookieName
     21      *            cookie的名称
     22      */
     23     public static void removeCookie( HttpServletRequest request, HttpServletResponse response,
     24             String cookieName) {
     25 
     26         Cookie[] cookies = request.getCookies();
     27         for (int i = 0; i < cookies.length; i++) {
     28             Cookie cookie = new Cookie(cookieName, null);
     29             cookie.setMaxAge(0);
     30             response.addCookie(cookie);
     31         }
     32     }
     33 
     34     /**
     35      * 
     36      * @desc 删除指定Cookie
     37      * @param response
     38      * @param cookie
     39      * @param domain
     40      */
     41     public static void removeCookie(HttpServletRequest request , HttpServletResponse response,
     42             String cookieName, String domain) {
     43         Cookie[] cookies = request.getCookies();
     44         for (int i = 0; i < cookies.length; i++) {
     45             Cookie cookie = new Cookie(cookieName, null);
     46             cookie.setMaxAge(0);
     47             cookie.setDomain(domain);
     48             response.addCookie(cookie);
     49         }
     50     }
     51 
     52     /**
     53      * 
     54      * @desc 根据Cookie名称得到Cookie的值,没有返回Null
     55      * @param name
     56      *            Cookie名称
     57      * @return
     58      */
     59     public static String getCookieValue(HttpServletRequest request ,String name) {
     60 
     61         Cookie cookie = getCookie(request , name);
     62         if (cookie != null) {
     63             return cookie.getValue();
     64         } else {
     65             return null;
     66         }
     67     }
     68 
     69     /**
     70      * 
     71      * @desc 根据Cookie名称得到Cookie对象,不存在该对象则返回Null
     72      * @param request
     73      * @param name
     74      */
     75     public static Cookie getCookie(HttpServletRequest request , String name) {
     76 
     77         Cookie cookies[] = request.getCookies();
     78         if (cookies == null || name == null || name.length() == 0) {
     79             return null;
     80         }
     81         Cookie cookie = null;
     82         for (int i = 0; i < cookies.length; i++) {
     83             if (!cookies[i].getName().equals(name)) {
     84                 continue;
     85             }
     86             cookie = cookies[i];
     87             if (request.getServerName().equals(cookie.getDomain())) {
     88                 break;
     89             }
     90         }
     91         return cookie;
     92     }
     93 
     94     /**
     95      * 
     96      * @desc 添加一条新的Cookie信息,默认有效时间为一个月
     97      * @param response
     98      * @param name
     99      * @param value
    100      */
    101     public static void setCookie(HttpServletResponse response, String name,
    102             String value) {
    103         setCookie(response, name, value, COOKIE_MAX_AGE);
    104     }
    105 
    106     /**
    107      * 
    108      * @desc 添加一条新的Cookie信息,可以设置其最长有效时间(单位:秒)
    109      * @param response
    110      * @param name
    111      * @param value
    112      * @param maxAge
    113      */
    114     public static void setCookie(HttpServletResponse response, String name,
    115             String value, int maxAge) {
    116         if (value == null) {
    117             value = "";
    118         }
    119         Cookie cookie = new Cookie(name, value);
    120         if (maxAge > 0) {
    121             cookie.setMaxAge(maxAge);
    122         } else {
    123             cookie.setMaxAge(COOKIE_MAX_AGE);
    124         }
    125         response.addCookie(cookie);
    126     }
    127 
    128 }
  • 相关阅读:
    [MySQL] 怎样使用Mysqlcheck来检查和修复, 优化表
    MySQL 5.6 & 5.7最优配置文件模板
    mysql5.6配置详解
    mysql 复制数据库
    mysql-binlog日志恢复数据库
    Memory Analyzer Tool定位Java heap space内存泄漏
    MySQL Flashback 闪回功能详解
    DisJSet:食物链(POJ 1182)
    BFS:Meteor Shower(POJ 3669)
    DFS:Curling 2.0(POJ 3009)
  • 原文地址:https://www.cnblogs.com/zfy0098/p/5098999.html
Copyright © 2020-2023  润新知