java CookieUtils cookie工具类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java CookieUtils cookie工具类相关的知识,希望对你有一定的参考价值。

  1 package com.mohecun.common.utils;
  2 
  3 import java.io.UnsupportedEncodingException;
  4 import java.net.URLDecoder;
  5 import java.net.URLEncoder;
  6 
  7 import javax.servlet.http.Cookie;
  8 import javax.servlet.http.HttpServletRequest;
  9 import javax.servlet.http.HttpServletResponse;
 10 
 11 
 12 /**
 13  * 
 14  * Cookie 工具类
 15  *
 16  */
 17 public final class CookieUtils {
 18 
 19     /**
 20      * 得到Cookie的值, 不编码
 21      * 
 22      * @param request
 23      * @param cookieName
 24      * @return
 25      */
 26     public static String getCookieValue(HttpServletRequest request, String cookieName) {
 27         return getCookieValue(request, cookieName, false);
 28     }
 29 
 30     /**
 31      * 得到Cookie的值,
 32      * 
 33      * @param request
 34      * @param cookieName
 35      * @return
 36      */
 37     public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {
 38         Cookie[] cookieList = request.getCookies();
 39         if (cookieList == null || cookieName == null) {
 40             return null;
 41         }
 42         String retValue = null;
 43         try {
 44             for (int i = 0; i < cookieList.length; i++) {
 45                 if (cookieList[i].getName().equals(cookieName)) {
 46                     if (isDecoder) {
 47                         retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");
 48                     } else {
 49                         retValue = cookieList[i].getValue();
 50                     }
 51                     break;
 52                 }
 53             }
 54         } catch (UnsupportedEncodingException e) {
 55             e.printStackTrace();
 56         }
 57         return retValue;
 58     }
 59 
 60     /**
 61      * 得到Cookie的值,
 62      * 
 63      * @param request
 64      * @param cookieName
 65      * @return
 66      */
 67     public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {
 68         Cookie[] cookieList = request.getCookies();
 69         if (cookieList == null || cookieName == null) {
 70             return null;
 71         }
 72         String retValue = null;
 73         try {
 74             for (int i = 0; i < cookieList.length; i++) {
 75                 if (cookieList[i].getName().equals(cookieName)) {
 76                     retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);
 77                     break;
 78                 }
 79             }
 80         } catch (UnsupportedEncodingException e) {
 81              e.printStackTrace();
 82         }
 83         return retValue;
 84     }
 85 
 86     /**
 87      * 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码
 88      */
 89     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
 90             String cookieValue) {
 91         setCookie(request, response, cookieName, cookieValue, -1);
 92     }
 93 
 94     /**
 95      * 设置Cookie的值 在指定时间内生效,但不编码
 96      */
 97     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
 98             String cookieValue, int cookieMaxage) {
 99         setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);
100     }
101 
102     /**
103      * 设置Cookie的值 不设置生效时间,但编码
104      */
105     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
106             String cookieValue, boolean isEncode) {
107         setCookie(request, response, cookieName, cookieValue, -1, isEncode);
108     }
109 
110     /**
111      * 设置Cookie的值 在指定时间内生效, 编码参数
112      */
113     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
114             String cookieValue, int cookieMaxage, boolean isEncode) {
115         doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
116     }
117 
118     /**
119      * 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)
120      */
121     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
122             String cookieValue, int cookieMaxage, String encodeString) {
123         doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);
124     }
125 
126     /**
127      * 删除Cookie带cookie域名
128      */
129     public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
130             String cookieName) {
131         doSetCookie(request, response, cookieName, "", -1, false);
132     }
133 
134     /**
135      * 设置Cookie的值,并使其在指定时间内生效
136      * 
137      * @param cookieMaxage cookie生效的最大秒数
138      */
139     private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
140             String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
141         try {
142             if (cookieValue == null) {
143                 cookieValue = "";
144             } else if (isEncode) {
145                 cookieValue = URLEncoder.encode(cookieValue, "utf-8");
146             }
147             Cookie cookie = new Cookie(cookieName, cookieValue);
148             if (cookieMaxage > 0)
149                 cookie.setMaxAge(cookieMaxage);
150             if (null != request) {// 设置域名的cookie
151                 String domainName = getDomainName(request);
152                 System.out.println(domainName);
153                 if (!"localhost".equals(domainName)) {
154                     cookie.setDomain(domainName);
155                 }
156             }
157             cookie.setPath("/");
158             response.addCookie(cookie);
159         } catch (Exception e) {
160              e.printStackTrace();
161         }
162     }
163 
164     /**
165      * 设置Cookie的值,并使其在指定时间内生效
166      * 
167      * @param cookieMaxage cookie生效的最大秒数
168      */
169     private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
170             String cookieName, String cookieValue, int cookieMaxage, String encodeString) {
171         try {
172             if (cookieValue == null) {
173                 cookieValue = "";
174             } else {
175                 cookieValue = URLEncoder.encode(cookieValue, encodeString);
176             }
177             Cookie cookie = new Cookie(cookieName, cookieValue);
178             if (cookieMaxage > 0)
179                 cookie.setMaxAge(cookieMaxage);
180             if (null != request) {// 设置域名的cookie
181                 String domainName = getDomainName(request);
182                 System.out.println(domainName);
183                 if (!"localhost".equals(domainName)) {
184                     cookie.setDomain(domainName);
185                 }
186             }
187             cookie.setPath("/");
188             response.addCookie(cookie);
189         } catch (Exception e) {
190              e.printStackTrace();
191         }
192     }
193 
194     /**
195      * 得到cookie的域名
196      */
197     private static final String getDomainName(HttpServletRequest request) {
198         String domainName = null;
199 
200         String serverName = request.getRequestURL().toString();
201         if (serverName == null || serverName.equals("")) {
202             domainName = "";
203         } else {
204             serverName = serverName.toLowerCase();
205             serverName = serverName.substring(7);
206             final int end = serverName.indexOf("/");
207             serverName = serverName.substring(0, end);
208             final String[] domains = serverName.split("\\.");
209             int len = domains.length;
210             if (len > 3) {
211                 // www.xxx.com.cn
212                 domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
213             } else if (len <= 3 && len > 1) {
214                 // xxx.com or xxx.cn
215                 domainName = "." + domains[len - 2] + "." + domains[len - 1];
216             } else {
217                 domainName = serverName;
218             }
219         }
220 
221         if (domainName != null && domainName.indexOf(":") > 0) {
222             String[] ary = domainName.split("\\:");
223             domainName = ary[0];
224         }
225         return domainName;
226     }
227 
228 }

 

以上是关于java CookieUtils cookie工具类的主要内容,如果未能解决你的问题,请参考以下文章

Java学习---Cookie工具

CookieUtils工具类

031-CookieUtils 工具类模板

CookieUtils-浏览器缓存工具类

CookieUtils

Cookie工具类 - CookieUtil.java