import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* @Package: com.xxxxx.common.util
* @Title: Cookie.java Create on 2012-1-25 下午5:23:52
* @Description:
*
* Cookie工具类,封装Cookie常用操作
*
* @author carrkevin
* @version v 0.1
*/
public class CookieHelper { /**
* 设置cookie有效期,根据需要自定义[本系统设置为30天]
*/
private final static int COOKIE_MAX_AGE = 1000 * 60 * 60 * 24 * 30; /**
*
* @desc 删除指定Cookie
* @param response
* @param cookie
*/
public static void removeCookie(HttpServletResponse response, Cookie cookie)
{
if (cookie != null)
{
cookie.setPath("/");
cookie.setValue("");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
} /**
*
* @desc 删除指定Cookie
* @param response
* @param cookie
* @param domain
*/
public static void removeCookie(HttpServletResponse response, Cookie cookie,String domain)
{
if (cookie != null)
{
cookie.setPath("/");
cookie.setValue("");
cookie.setMaxAge(0);
cookie.setDomain(domain);
response.addCookie(cookie);
}
} /**
*
* @desc 根据Cookie名称得到Cookie的值,没有返回Null
* @param request
* @param name
* @return
*/
public static String getCookieValue(HttpServletRequest request, String name)
{
Cookie cookie = getCookie(request, name);
if (cookie != null)
{
return cookie.getValue();
}
else
{
return null;
}
} /**
*
* @desc 根据Cookie名称得到Cookie对象,不存在该对象则返回Null
* @param request
* @param name
*/
public static Cookie getCookie(HttpServletRequest request, String name)
{
Cookie cookies[] = request.getCookies();
if (cookies == null || name == null || name.length() == 0)
return null;
Cookie cookie = null;
for (int i = 0; i < cookies.length; i++)
{
if (!cookies[i].getName().equals(name))
continue;
cookie = cookies[i];
if (request.getServerName().equals(cookie.getDomain()))
break;
} return cookie;
} /**
*
* @desc 添加一条新的Cookie信息,默认有效时间为一个月
* @param response
* @param name
* @param value
*/
public static void setCookie(HttpServletResponse response, String name, String value)
{
setCookie(response, name, value, COOKIE_MAX_AGE);
} /**
*
* @desc 添加一条新的Cookie信息,可以设置其最长有效时间(单位:秒)
* @param response
* @param name
* @param value
* @param maxAge
*/
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge)
{
if (value == null)
value = "";
Cookie cookie = new Cookie(name, value);
if(maxAge!=0){
cookie.setMaxAge(maxAge);
}else{
cookie.setMaxAge(COOKIE_MAX_AGE);
}
cookie.setPath("/");
response.addCookie(cookie);
}
}

Cookie 操作工具类的更多相关文章

  1. 170403、java 版cookie操作工具类

    package com.rick.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; imp ...

  2. java cookie 工具类

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

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

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

  4. Cookie工具类

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

  5. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  6. Cookie工具类 - CookieUtil.java

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

  7. [转载]C# FTP操作工具类

    本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...

  8. 拼音操作工具类 - PinyinUtil.java

    拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...

  9. Cookie帮助类

    using System; using System.Collections.Generic; using System.Text; using System.Web; namespace AIMSC ...

随机推荐

  1. Linq技术四:动态Linq技术 -- Linq.Expressions

    前面介绍了Linq的三个方面应用:Linq to SQL, Linq to XML和Linq to Object,这篇介绍一下动态Linq的实现方式及应用场景. 命名空间: System.Linq; ...

  2. WPF中的CheckBox的_ (underscore / 下划线)丢失

    今天在项目中遇到check box的Content的内容缺少'_', 原因是WPF的ContentPresenter默认会把'_'作为加速键的转义字符.  比方CheckBox的content为&qu ...

  3. C# 实现 Hyper-V 虚拟机 管理

    原文:C# 实现 Hyper-V 虚拟机 管理 Hyper-V WMI Provider 工具类如下: "; break; } return status; } } /// <summ ...

  4. CentOS 7 结构体GCC 4.8.2 32位编译环境

    centos 7 结构体gcc 32位编译环境 1介绍 1.1背景 学习新 C++ 2011和C11标准. 1.2使用软件 CentOS 7(Linux version 3.10.0-123.el7. ...

  5. Object.prototype.toString &amp; typeof

    Object.prototype.toString & typeof Object.prototype.toString 获取某个对象属于哪种内置类型 typeof  得到某个对象的类型 差别 ...

  6. 淘宝code

    淘宝code 相信大家都听说过GitHub,也有很多人在用,但是GitHub毕竟在国外,速度不是很给力,而且安装过程也是很漫长.今天来给大家介绍一个国内的免费的开源项目平台,当然也是一个SVN版本控制 ...

  7. 地图坐标Base64转换数字坐标

    测试地图源码==百读Demo<html> <head> </head> <body> <script type="text/javasc ...

  8. Mysql高级之权限检查原理

    原文:Mysql高级之权限检查原理 用户进行数据库操作分为两步: 1 是否有权限连接,根据host,name,password: 2 是否有权限进行CURD: 图示解说: 关于用户权限在哪里进行存放? ...

  9. 开始DDD

    如何开始DDD(完) 连续写了两篇文章,这一篇我想是序的完结篇了.结合用户注册的例子再将他简单丰富一下.在这里只添加一个简单需求,就是用户注册成功后给用户发一封邮件.补充一下之前的代码 public ...

  10. 使用response实现文件下载功能

    response.setContentType("text/html;charset=utf-8");  response.setCharacterEncoding("u ...