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;

/**

*

* Cookie 工具类

*

*/

public final class CookieUtils {

/**

* 得到Cookie的值, 不编码

*

* @param request

* @param cookieName

* @return

*/

public static String getCookieValue(HttpServletRequest request, String cookieName) {

return getCookieValue(request, cookieName, false);

}

/**

* 得到Cookie的值,

*

* @param request

* @param cookieName

* @return

*/

public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {

Cookie[] cookieList = request.getCookies();

if (cookieList == null || cookieName == null) {

return null;

}

String retValue = null;

try {

for (int i = 0; i < cookieList.length; i++) {

if (cookieList[i].getName().equals(cookieName)) {

if (isDecoder) {

retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");

} else {

retValue = cookieList[i].getValue();

}

break;

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return retValue;

}

/**

* 得到Cookie的值,

*

* @param request

* @param cookieName

* @return

*/

public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {

Cookie[] cookieList = request.getCookies();

if (cookieList == null || cookieName == null) {

return null;

}

String retValue = null;

try {

for (int i = 0; i < cookieList.length; i++) {

if (cookieList[i].getName().equals(cookieName)) {

retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);

break;

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return retValue;

}

/**

* 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue) {

setCookie(request, response, cookieName, cookieValue, -1);

}

/**

* 设置Cookie的值 在指定时间内生效,但不编码

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, int cookieMaxage) {

setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);

}

/**

* 设置Cookie的值 不设置生效时间,但编码

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, boolean isEncode) {

setCookie(request, response, cookieName, cookieValue, -1, isEncode);

}

/**

* 设置Cookie的值 在指定时间内生效, 编码参数

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, int cookieMaxage, boolean isEncode) {

doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);

}

/**

* 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)

*/

public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue, int cookieMaxage, String encodeString) {

doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);

}

/**

* 删除Cookie带cookie域名

*/

public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName) {

doSetCookie(request, response, cookieName, "", -1, false);

}

/**

* 设置Cookie的值,并使其在指定时间内生效

*

* @param cookieMaxage cookie生效的最大秒数

*/

private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {

try {

if (cookieValue == null) {

cookieValue = "";

} else if (isEncode) {

cookieValue = URLEncoder.encode(cookieValue, "utf-8");

}

Cookie cookie = new Cookie(cookieName, cookieValue);

if (cookieMaxage > 0)

cookie.setMaxAge(cookieMaxage);

if (null != request) {// 设置域名的cookie

String domainName = getDomainName(request);

System.out.println(domainName);

if (!"localhost".equals(domainName)) {

cookie.setDomain(domainName);

}

}

cookie.setPath("/");

response.addCookie(cookie);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 设置Cookie的值,并使其在指定时间内生效

*

* @param cookieMaxage cookie生效的最大秒数

*/

private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName, String cookieValue, int cookieMaxage, String encodeString) {

try {

if (cookieValue == null) {

cookieValue = "";

} else {

cookieValue = URLEncoder.encode(cookieValue, encodeString);

}

Cookie cookie = new Cookie(cookieName, cookieValue);

if (cookieMaxage > 0)

cookie.setMaxAge(cookieMaxage);

if (null != request) {// 设置域名的cookie

String domainName = getDomainName(request);

System.out.println(domainName);

if (!"localhost".equals(domainName)) {

cookie.setDomain(domainName);

}

}

cookie.setPath("/");

response.addCookie(cookie);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 得到cookie的域名

*/

private static final String getDomainName(HttpServletRequest request) {

String domainName = null;

String serverName = request.getRequestURL().toString();

if (serverName == null || serverName.equals("")) {

domainName = "";

} else {

serverName = serverName.toLowerCase();

serverName = serverName.substring(7);

final int end = serverName.indexOf("/");

serverName = serverName.substring(0, end);

final String[] domains = serverName.split("\\.");

int len = domains.length;

if (len > 3) {

// www.xxx.com.cn

domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];

} else if (len <= 3 && len > 1) {

// xxx.com or xxx.cn

domainName = "." + domains[len - 2] + "." + domains[len - 1];

} else {

domainName = serverName;

}

}

if (domainName != null && domainName.indexOf(":") > 0) {

String[] ary = domainName.split("\\:");

domainName = ary[0];

}

return domainName;

}

}

关于Cookie的 工具类的更多相关文章

  1. 自定义的操作Cookie的工具类

    可以在SpringMVC等环境中使用的操作Cookie的工具类 package utils; import java.io.UnsupportedEncodingException; import j ...

  2. java与javascript对cookie操作的工具类

    Java对cookie的操作 package cn.utils; import java.util.HashMap; import java.util.Map; import javax.servle ...

  3. java cookie 工具类

    package com.xxx.xxx.xxx.xxx; import java.net.URLDecoder; import java.net.URLEncoder; import javax.se ...

  4. cookie工具类,解决servlet3.0以前不能添加httpOnly属性的问题

    最近在解决XSS注入的问题,由于使用的servlet版本是2.5,不支持httpOnly的属性,故做了个工具类来实现cookie的httpOnly的功能.全类如下: /** * cookie工具类,解 ...

  5. Cookie工具类

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

  6. Cookie工具类 - CookieUtil.java

    Cookie工具类,提供Cookie的创建.获取.删除方法. 源码如下:(点击下载 -CookieUtil.java ) import javax.servlet.http.Cookie; impor ...

  7. Java Cookie工具类

    1.Cookie跨域 Cookie不能跨顶级域名访问,但是二级域名可以共享Cookie,所以要实现跨域,有一定的局限性. 2.代码 package com.DingYu.Cookie; import ...

  8. 180425、cookie工具类

    package com.thinkgem.jeesite.common.utils; import java.io.UnsupportedEncodingException; import java. ...

  9. 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求

    近期更新了一下HttpClientUtil工具类代码,主要是加入了一个參数HttpContext,这个是用来干嘛的呢?事实上是用来保存和传递Cookie所须要的. 由于我们有非常多时候都须要登录.然后 ...

随机推荐

  1. DotNetBar for Windows Forms 12.1.0.0_冰河之刃重打包版 原创发布

    关于 DotNetBar for Windows Forms 12.1.0.0_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

  2. 初探bootstarp

    目录1.下载Bootstrap2.引入Bootstrap相关的css.js3.一个最简单的Bootstrap页面4.浏览器支持 1.下载BootstrapBootstrap官网下载地址Bootstra ...

  3. Android高效加载大图、多图解决方案,有效避免程序OOM

    高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...

  4. VS2012 调试时 局部变量显示不全的问题解决

    在工程上右键,打开属性页,配置属性——C/C++——优化,将优化改为“已禁用/Od“

  5. Delphi编译的程序如何获取管理员权限

    1.制作manifest文件 <?xml version="1.0" encoding="UTF-8" standalone="yes" ...

  6. 你可能不知道的iOS冷知识——#pragma

    Mattt Thompson撰写. Zihan Xu翻译. 发布于2012年10月1日 #pragma 声明是彰显 Objective-C 工艺的标志之一.虽然 #pragma 最初的目的是为了使得源 ...

  7. linux清理内存

    free -m echo 1 > /proc/sys/vm/drop_caches

  8. C++调用V8与JS交互

    C++访问JS函数 C++部分: /** * COMPILE foo.js AT THE FIRST COMMAND PROMPT TO RUN foo.js */ #include <v8.h ...

  9. OC calendar 实践中的那些坑

    博客已经迁移到www.chjsun.top 最近想做一个万年历似的东西,因为需要把农历也添加进去,就想直接调用苹果自带的api,这样还方便一些, 搜索了一下,苹果对于时间的处理,还是提供了很多选择给我 ...

  10. windows10和ubuntu16.04双系统下时间不对的问题 ZT

    最近装了windows10和ubuntu16.04双系统,仍然出现了喜闻乐见的老问题,装完后,在windows下时区不对,之前的老办法是: sudo gedit /etc/default/rcS ut ...