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