Cookie 操作工具类
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 操作工具类的更多相关文章
- 170403、java 版cookie操作工具类
package com.rick.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; imp ...
- java cookie 工具类
package com.xxx.xxx.xxx.xxx; import java.net.URLDecoder; import java.net.URLEncoder; import javax.se ...
- cookie工具类,解决servlet3.0以前不能添加httpOnly属性的问题
最近在解决XSS注入的问题,由于使用的servlet版本是2.5,不支持httpOnly的属性,故做了个工具类来实现cookie的httpOnly的功能.全类如下: /** * cookie工具类,解 ...
- Cookie工具类
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet ...
- Code片段 : .properties属性文件操作工具类 & JSON工具类
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...
- Cookie工具类 - CookieUtil.java
Cookie工具类,提供Cookie的创建.获取.删除方法. 源码如下:(点击下载 -CookieUtil.java ) import javax.servlet.http.Cookie; impor ...
- [转载]C# FTP操作工具类
本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...
- 拼音操作工具类 - PinyinUtil.java
拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...
- Cookie帮助类
using System; using System.Collections.Generic; using System.Text; using System.Web; namespace AIMSC ...
随机推荐
- MVC4新功能...压缩和合并js文件和样式文件
1.在App_Start文件夹中BundleConfig.cs类中添加相应的文件 1.1bundles.Add(new ScriptBundle("~/bundles/adminJs&quo ...
- JSR303 Bean Validation 技术规范特性概述
概述 Bean Validation 规范 Bean 是 Java Bean 的缩写,在 Java 分层架构的实际应用中,从表示层到持久化层,每一层都需要对 Java Bean 进行业务符合性验证,如 ...
- 从久负盛名的GoDaddy开发革命来看Node.js的风靡程度
英文原文连接:http://venturebeat.com/2015/02/09/godaddy-nodejitsu/ 网站主机托管公司GoDaddy将要进一步通过新的开发工具来提升自身能力.最新消息 ...
- Session变量不能转移到下页.解决: session.use_trans_sid = 1
附:文摘 ============================================================ 在PHP使用SESSION的朋友可能会碰到这么一个问题.SESSIO ...
- Linux Shell脚本入门--Uniq命令
uniq uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用.也就是说,为了使uniq起作用,所有的重复行必须是相邻的. uniq语法 [root@www ~]# uniq [ ...
- 高效DevOps的10项实践
高效DevOps的10项实践 原文链接: http://www.drdobbs.com/architecture-and-design/top-10-practices-for-effective-d ...
- 【转】Android折叠效果实现案例
源文:http://mobile.51cto.com/abased-401983.htm 为了使界面的效果更加绚丽,体验效果更佳,往往需要开发者们自行开发新的界面效果,在这里,我将奉上各种实现折叠效果 ...
- beanutils获取带参数get方法
public Employee getEmployee(int index) { return new Employee(); } 1.PropertyUtils.getIndex ...
- validate大表单验证
Vaidate 插件 在前端开发中, 我们会遇到大表单的验证和组合成JSON, 这是一项巨大的任务, 如果都通过 手动编写低级代码来实现 50+ input类型的验证和复杂JSON的组装, 这无疑是异 ...
- Visual Studio 2014
开发 ASP.NET vNext 初步总结(使用Visual Studio 2014 CTP1) 2014-06-06 18:04 by 梁逸晨, 2149 阅读, 29 评论, 收藏, 编辑 新特性 ...