package com.willow.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* CookieUtil用来操作cookie的存取
* @author GetInstance
*
*/
public class CookieUtil {
/**
* 添加cookie
* @param name cookie的key
* @param value cookie的value
* @param domain domain
* @param path path
* @param maxage 最长存活时间 单位为秒
* @param response
*/
public static void addCookie(String name ,String value,String domain,
int maxage,String path, HttpServletResponse response){
Cookie cookie = new Cookie(name,value);
if(domain!=null){
cookie.setDomain(domain);
}
cookie.setMaxAge(maxage);
cookie.setPath(path);
response.addCookie(cookie);
} /**
* 往根下面存一个cookie
* * @param name cookie的key
* @param value cookie的value
* @param domain domain
* @param maxage 最长存活时间 单位为秒
* @param response
*/
public static void addCookie(String name ,String value,String domain,
int maxage, HttpServletResponse response){
addCookie(name, value,domain, maxage, "/" , response);
} /**
* 从cookie值返回cookie值,如果没有返回 null
* @param req
* @param name
* @return cookie的值
*/
public static String getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if (cookies == null) return null;
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(name)) {
return cookies[i].getValue();
}
}
return null;
} public static void removeCookie(String name, String domain, HttpServletRequest request, HttpServletResponse response) {
String cookieVal = getCookie(request,name);
if(cookieVal!=null){
CookieUtil.addCookie(name, null, domain, 0, response);
}
} public static void removeCookie(String name, HttpServletRequest request, HttpServletResponse response) {
CookieUtil.removeCookie(name, ".dhgate.com", request, response);
}
}

Java_CookieUtil的更多相关文章

随机推荐

  1. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  2. AOJ673 聪明的输入法(字典树)

    #include<cstdio> #include <cstdlib> #include <cstring> #include <iostream> # ...

  3. slf4i + logback 配置

    一.所需jar包: slf4j-api-1.6.1.jar logback-classic-0.9.24.jar logback-core-0.9.24.jar 二.logback.xml配置示例: ...

  4. angularjs实战

    1.指令  transclude 保留原来的内容 replace 去掉<my-directive>指令 <script src="http://apps.bdimg.com ...

  5. js判断手机端Android手机还是iPhone手机

    /*判断当前设备是平板.安卓.苹果设备*/ <script type="text/javascript"> function fBrowserRedirect(){ v ...

  6. hdu 2891 中国剩余定理

    从6点看到10点,硬是没算出来,早知道玩游戏去了,艹,明天继续看 不爽,起来再看,终于算是弄懂了,以后超过一个小时的题不会再看了,不是题目看不懂,是水平不够 #include<cstdio> ...

  7. WPF之MVVM(Step1)——自己实现ICommand接口

    开发WPF应用程序,就不得不提MVVM.下面偶将展示MVVM中简单的实现,其中主要在于ICommand的实现上,不过这种实现方式,应该不会有多少人在开发中使用,在此仅作学习使用. 准备: 界面绘制,简 ...

  8. ARM伪指令,王明学learn

    ARM伪指令 在ARM汇编语言程序中里,有一些特殊指令助记符与指令系统的助记符不同,没有相对应的操作码,通常称这些特殊指令助记符为伪指令,他们所完成的操作称为伪操作.伪指令在元程序中的作用是为完成汇编 ...

  9. android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法

      http://blog.csdn.net/fth826595345/article/details/9208771 主题 TextView 转载请注明出处: http://blog.csdn.ne ...

  10. LeetCode——Same Tree(判断两棵树是否相同)

    问题: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...