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. 点击鼠标获取元素ID

    原文:点击鼠标获取元素ID public partial class Form1 : Form { public Form1() { InitializeComponent(); } private ...

  2. linq 分组求和

    static void Main()         {             DataTable dt = new DataTable();             dt.Columns.Add( ...

  3. 创建GitHub技术博客

    创建GitHub技术博客全攻略 githubio技术博客网站生成 说明: 首先,你需要注册一个 github 账号,最好取一个有意义的名字,比如姓名全拼,昵称全拼,如果被占用,可以加上有意义的数字.本 ...

  4. Codeforces 113A-Grammar Lessons(实现)

    A. Grammar Lessons time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  5. 安卓MonkeyRunner源码分析之启动

    在工作中因为要追求完成目标的效率,所以更多是强调实战,注重招式,关注怎么去用各种框架来实现目的.但是如果一味只是注重招式,缺少对原理这个内功的了解,相信自己很难对各种框架有更深入的理解. 从几个月前开 ...

  6. JS判断字符串是否全为中文

    //第一种代码(全为中文则返回"true",不全为中文则返回"false"): <script language="javascript&quo ...

  7. PDFBox 介绍

    根据官网的介绍可知,PDFBox是一个用来处理PDF文档的开源的Java工具包.这个项目运行创建PDF文档.对已有文档进行操作并且能够从文档中提取内容.它也包含了几个命令行工具.还有一点很重要,它是开 ...

  8. Android项目---webView

    之前用webView的时候,只知道它可以加载出html页面,竟然不知道,它也可以与js进行交互. WebView是一个网络视图,能加载显示网页,可以将它看作一个浏览器.它使用WebKit渲染引擎加载显 ...

  9. linux 启动oracle报cannot restore segment prot after reloc: Permission denied

    error while loading shared libraries: $ORACLE_HOME/lib/libnnz10.so: cannot restore segment prot afte ...

  10. java中dao层的通用层,通过反射机制,操作数据库的增删改,适用的范围是不包含属性类

    这里首先必须注意的是:类的类名.字段必须与数据库中的表名和字段名保持一致,否则无法通过反射机制作出通用层 /** * 学生信息类,数据库中的StuInfo表 * */public class StuI ...