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 ...
随机推荐
- 拷贝构造函数,深拷贝,大约delete和default相关业务,explicit,给定初始类,构造函数和析构函数,成员函数和内联函数,关于记忆储存,默认参数,静态功能和正常功能,const功能,朋友
1.拷贝构造 //拷贝构造的规则,有两种方式实现初始化. //1.一个是通过在后面:a(x),b(y)的方式实现初始化. //2.另外一种初始化的方式是直接在构造方法里面实现初始化. 案比例如以 ...
- Unix命令操作
基本命令 [ man 查看 ]--万能命令 1.ls 列出文件 (-al) 2.cd 转换目录 3.mkdir 建立新目录 4.cp 拷贝文件 (-R) 5.rm 删除文件 (-rf) 6.mv 移动 ...
- Linux环境编程相关的文章
Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...
- jrtplib的使用
JRTPLIB库的使用 文档: http://research.edm.uhasselt.be/jori/jrtplib/documentation/index.html 一些介绍: http://d ...
- APP-随身听
简单到复杂听你的专属音响界,听金融.听物业,听新闻和其他节目专辑,简要介绍了新的音频应用,给你不一样的聆听体验.还记得老歌做?这里有.您留声机的一部分!很简单的音频应用,随时随地与此应用程序来听你的私 ...
- 第三记“晋IT”分享成长沙龙
2014年8月17日下午4点-7点,第三期"晋IT"分享成长沙龙在太原大自然蒙特梭利幼儿园多功能厅成功举办. 8月17日下午两点.小编领先来到场地,提前探訪一下准备情况. &quo ...
- td中的值自动换行
设置td中的值自动换行在<td ></td> 中加上这样一句代码,可以简省设置,使长字符串换行.而不用设置width,height. style="word-wrap ...
- 初识Python-web框架的这两天
前段时间打算学习python,其实时间蛮紧张的,看看文字教程,累了就看视频教程.算是把基本的语法过了一遍,但是OOP就费了好大的气力 ,C里有的对象,继承,等等等,还算能理解.不过高级点的就理解起来吃 ...
- ventBroker简单实现
C#编程实践—EventBroker简单实现 前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.n ...
- 笔试 - 高德软件有限公司python问题 和 答案
高德软件有限公司python问题 和 答案 本文地址: http://blog.csdn.net/caroline_wendy/article/details/25230835 by Spike 20 ...