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 }
时间: 2024-10-11 05:00:17

java CookieUtils cookie工具类的相关文章

Cookie工具类 - CookieUtil.java

Cookie工具类,提供Cookie的创建.获取.删除方法. 源码如下:(点击下载 -CookieUtil.java ) import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Cookie工具类 * */ public class CookieUtil { private CookieU

180425、cookie工具类

package com.thinkgem.jeesite.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.

Cookie工具类(获取cookie,设置cookie)

import java.io.UnsupportedEncodingException; import java.net.URLDecoder;import java.net.URLEncoder; import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;i

Cookie工具类

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 /** 9

UrlUtils工具类,Java URL工具类,Java URL链接工具类

UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ?Copyright 蕃薯耀 2017年7月15日 http://www.cnblogs.com/fanshuyao/ Java代码   import java.util.Ha

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ?Copyright 蕃薯耀 2017年9月13日 http://www.cnblogs.com/fanshuyao/ 直接上代码: import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.ref

[精品] 收集的27个java开发常用工具类.基本满足开发需求

原文:[精品] 收集的27个java开发常用工具类.基本满足开发需求 源代码下载地址:http://www.zuidaima.com/share/1596028005993472.htm 最近从网上收集的java开发常用的工具类,分享给大家.基本满足开发需求.推荐给热爱最代码以及java的牛牛们.   每个类都有注释的,欢迎大家可以下载使用. 字符编码:CharTools, base64:Base64 *.java Md5加密:  MD5*.java 上传:*Uploader* 生成缩略图类:T

java MD5数据加密工具类

package com.wetuo.util; import java.security.MessageDigest; /**  * 数据加密工具类  * @author wzp  *  */ public class DataUtil { public static String md5(String str) { StringBuffer buffer = new StringBuffer(); char[] chars = { '0', '1', '2', '3', '4', '5', '

Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类

Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类   =========================== ?Copyright 蕃薯耀 2017年9月25日 http://www.cnblogs.com/fanshuyao/ 一.问题描述 很多对外网站的某些内容都需要过滤敏感词,避免政治与色@情上的问题. 二.解决方案 使用词库进行匹配过滤成 * (星号) Java 敏感词工具类及敏感词词库见附件. 1.下载后,有几个类,主要为WordFilter 这个工具类,使用方法如下