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

170403、java 版cookie操作工具类的更多相关文章

  1. 170404、java版ftp操作工具类

    package com.rick.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...

  2. Cookie 操作工具类

    import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet ...

  3. java/javascript 时间操作工具类

    一.java 时间操作工具类 import org.springframework.util.StringUtils; import java.text.ParseException; import ...

  4. java:数组操作工具类 java.util.Arrays包 主要方法详解

    Arrays类位于Java.util包下,是一个对数组操作的工具类,现将Arrays类中的方法做一个总结(JDK版本:1.6.0_34).Arrays类中的方法可以分为八类: sort(对数组排序) ...

  5. Java版InfluxDB工具类

    InfluxDB工具类 package com.influxdb.test; import java.util.Map; import org.influxdb.InfluxDB; import or ...

  6. JAVA 文本 TXT 操作工具类

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...

  7. 170405、java版MD5工具类

    package com.rick.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  8. Java IO(文件操作工具类)

    FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...

  9. java版RSA工具类

    /** * RSA算法加密/解密工具类 */ public class RSAUtils { private static final Logger LOGGER = LoggerFactory.ge ...

随机推荐

  1. vue使用axios,进行网络请求

    1.首先自己创建一个组件: https://www.cnblogs.com/fps2tao/p/9559291.html 2.安装:axios(可以npm安装,也可以下载js引入文件) npm ins ...

  2. 【Android】14.0 第14章 内部存储与外部SD卡存储—本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 Android使用的文件系统是基于Linux的文件系统,在Android应用程序中,开发人员既可以建立和访问程序自 ...

  3. Android从无知到有知——NO.5

    今天整一下利用广播实现ip拨号. 这一块主要用到的知识是android四大组件之中的一个的broadcast   receiver(广播接收者).那么它接收什么东东呢,就是我们所无谓的一个个的事件,比 ...

  4. Node.js用fs.renameSync报cross-device link not permitted错

    转自: http://blog.csdn.net/starrexstar/article/details/8048722 今天把 Manuel Kiessling 的[The Node Beginne ...

  5. dubbo异步调用三种方式

    异步通讯对于服务端响应时间较长的方法是必须的,能够有效地利用客户端的资源,在dubbo中,消费端<dubbp:method>通过 async="true"标识. < ...

  6. [转]__cdecl与__stdcall

    来自Programming Windows 5th Edition The WinMain function is given a type of WINAPI (as is every Window ...

  7. Android中<uses-sdk>属性和target属性分析

    1. 概要 <uses-sdk> 用来描述该应用程序可以运行的最小和最大API级别,以及应用程序开发者设计期望运行的平台版本.通过在manifest清单文件中添加该属性,我们可以更好的控制 ...

  8. 继承log4.net的类

    using System; using System.Diagnostics; [assembly: log4net.Config.XmlConfigurator(Watch = true)] nam ...

  9. 使用info命令查看Redis信息和状态

    redis-cli连接服务器后,使用info命令查看Redis信息和状态: ? 1 info 其中memory段显示了redis的内存使用状态. 以下内容复制自:http://redisdoc.com ...

  10. 用javascript技术读取注册表中软件安装位置并启动本地软件

    1.首先读取注册表中本地软件安装的位置,如果未安装则无就跳转到下载页面. 2.启动软件,关闭页面. 3.如报错提示. <SCRIPT language=javascript>  <! ...