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 ...
随机推荐
- Cocos2d-x之MenuItem
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- MySQL检查连接的最大数量和改变的最大连接数
版权声明:本文博主原创文章.博客,未经同意不得转载.
- Cocos2d-x3.0 TestCPP文件夹的注意事项
1.不多说了,重力加速度. 2.ActionMangerTest:此Test它是由导演来展示,以获得集体诉讼经理ActionManager类别,操作控制节点. ①CrashTest:破坏demo,毁. ...
- outlook 会议室
原文:outlook 会议室 但是,里面的方法只能用于发送普通电子邮件.如果要发起会议之类的特殊邮件的话,可以C#调用Outlook API,自身的API. 创建项目后,为它添加.NET引用:“Mic ...
- leetcode第十题--Regular Expression Matching
Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single c ...
- SQL Server查看表结构及视图,适合开发者使用,简单易用
SELECT * FROM INFORMATION_SCHEMA.TABLES SELECT * FROM INFORMATION_SCHEMA.COLUMNS 查看执行结果
- 利用sqlclr实现数据库服务器端数据加密解密
在公司中一同事用sqlclr写数据迁移自动化执行脚本,发现他在执行脚本时对数据进行了加密. 个人觉得利用sqlclr对数据进行加密是一个解决数据网络安全传输的不错的方案. 以下是一个小的案例: --- ...
- WebActivator的实现原理详解
WebActivator的实现原理详解 文章内容 上篇文章,我们分析如何动态注册HttpModule的实现,本篇我们来分析一下通过上篇代码原理实现的WebActivator类库,WebActivato ...
- 关于ListView中convertView的缓存个数的探究
在面试的时候经常会被问到一个有关ListView的问题:一个ListView的高度最多可以显示5个item,但是却有20条数据要显示,问最多会有多少个convertView会被复用?或者如在ListV ...
- Bootstrap 模态框(也可以说的弹出层)
最近在尝试使用bootstrap的模态框 使用模态框主要要引入一下几个js和css: bootstrap.css jquery.1.9.1.js(这个可以灵活选择) bootstrap.js html ...