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 ...
随机推荐
- UVA11627-Slalom(二分法)
题目链接 题意:有n个宽为w的旗门,第i个旗门左端的坐标为(xi, yi),对于全部1 <= i < n满足yi < y(i+1).你有s双滑雪板,第j双的速度为sj(垂直向下的速度 ...
- veridata实验举例(2)验证表BONUS与表SALGRADE两节点同步情况
veridata实验举例(2)验证表BONUS与表SALGRADE两节点同步情况 续接前几篇文章: 1.GoldenGate配置(一)之单向复制配置 地址:点击打开链接 2.GoldenGate配置( ...
- NET中异常处理的最佳实践
NET中异常处理的最佳实践 本文翻译自CodeProject上的一篇文章,原文地址. 目录 介绍 做最坏的打算 提前检查 不要信任外部数据 可信任的设备:摄像头.鼠标以及键盘 “写操作”同样可能失效 ...
- Linux开源模块迁移概述暨交叉编译跨平台移植总结--从《嵌入式Linux驱动模板简洁和工程实践》
本文摘录<嵌入式Linux驱动模板简洁和工程实践>一本书"开发和调试技术". Linux强大的是,有那么多的开源项目可以使用.通常非常需要可以通过寻找相关的源模块被定义 ...
- 在 VS2013的ASPNET站点开发中用 xheditor v1.1.13 作为HTML编辑器
要用vs2013开发一个博客站点,.net framework 4,须要一个HTML编辑器作为写文章的工具.经多方试用,排除了dotnettextbox.kceditor.认为xheditor ...
- 告别alert,拥抱console
记得学习javascript的第一个demo就是alert("Hello World");可是学习接触javascript这么长时间了还是在alert,因为javascript调 ...
- Android_WebServices_介绍
本博文为子墨原创,转载请注明出处! http://blog.csdn.net/zimo2013/article/details/38036289 1.WebService的介绍 WebService为 ...
- web浏览器中javascript
1.异步载入一个js代码function loadasync(url) { var head = document.getElementsByTagName("head")[0]; ...
- hudson任务配置说明
hudson任务配置说明 Discard Old Builds:hudson默认保留过去的构建,勾选此选项,则可以设置构建记录的有效期: (帮助:这里控制着您想要在hudson所在的磁盘把构建记录存储 ...
- Oracle自主事务处理
--当一个子程序调用另外一个子程序时,事务提交或回滚都将影响两个子程序 --为防止一个子程序的事务影响其他子程序,可以将这个子程序标记为自主事务处理 --这样自主事务成为一个独立的事务处理,不影响其他 ...