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. HTML5_Canvas_属性、定义及方法

    一.简单图形,整套的属性和方法专门用于绘制矩形:1.fillStyle可以设置为CSS颜色.一个图案或一种颜色渐变.fillStyle默认是纯黑色,你可以设置成你喜欢的任意颜色.只要页面打开着,每个绘 ...

  2. 11g Physical Standby配置

    一,准备   Database DB_UNIQUE_NAME Oracle Net Service Name Primary PROD PROD Physical standby PRODDG PRO ...

  3. WCF 回调中操作线程

    回调的类 [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = fals ...

  4. zoj 3882 博弈 *

    看了半天约数居然包括1,水了 #include<cstdio> #include<iostream> #include<algorithm> #include< ...

  5. [荐]jquery插件开发入门

    http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html $.fn.myPlugin = function() { //在这里面,this指 ...

  6. BZOJ 1861: [Zjoi2006]Book 书架 splay

    1861: [Zjoi2006]Book 书架 Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书 ...

  7. 出来ios顶部导航掉下来问题

    <script type="text/javascript"> setposition(); function setposition(){ var ua = navi ...

  8. 彻底删除Kafka中的topic

    1.删除kafka存储目录(server.properties文件log.dirs配置,默认为"/tmp/kafka-logs")相关topic目录 2.Kafka 删除topic ...

  9. BurpSuite导出log配合SQLMAP批量扫描注入点

    sqlmap可以批量扫描包含有request的日志文件,而request日志文件可以通过burpsuite来获取, 因此通过sqlmap结合burpsuite工具,可以更加高效的对应用程序是否存在SQ ...

  10. Loadrunner中参数化实战(7)-Unique+Each iteration

    参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Unique+Each iteration 设置迭代4次Action 结果如下: