package com.ulearning.ulms.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.ulearning.ulms.core.utils.Constants; public class CookieUtil {
/*
* 从给定的request中查找cookie
*/
public static String getCookie(HttpServletRequest request,String cookieName){
String rt=null;
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{ for (int i = 0; i < cookies.length; i++)
{
Cookie c = cookies[i];
if(c.getName().equalsIgnoreCase(cookieName))
{
rt= c.getValue();
break;
}
}
}
try {
if(rt !=null){
rt = URLDecoder.decode(rt,"utf-8");
}else{
rt = (String)request.getAttribute(cookieName);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rt;
}
/**
* 删除,某一个cookie
* @param request
* @param cookieName
* @return
*/ public static void clearCookie(HttpServletRequest request,HttpServletResponse response,String cookieName){
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{ for (int i = 0; i < cookies.length; i++)
{
Cookie c = cookies[i];
if(!c.getName().equalsIgnoreCase(cookieName))
{
c.setValue(null);
c.setMaxAge(0);
c.setPath("/");
response.addCookie(c);
break;
}
}
}
} public static int getCookieInt(HttpServletRequest request,String cookieName){
int res = 0;
String rt=null;
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{ for (int i = 0; i < cookies.length; i++)
{
Cookie c = cookies[i];
if(c.getName().equalsIgnoreCase(cookieName))
{
rt= c.getValue();
break;
}
}
}
try {
if(rt !=null)
{
rt = URLDecoder.decode(rt,"utf-8");
res = Integer.parseInt(rt);
}else{
rt = (String)request.getAttribute(cookieName);
res = rt!=null ? Integer.parseInt(rt) : 0;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
} public static void clearCookie(HttpServletRequest request,HttpServletResponse response){
String rt=null;
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{
for (int i = 0; i < cookies.length; i++)
{
Cookie c = cookies[i];
if(!c.getName().equalsIgnoreCase(Constants.SHOPPING_CART_KEY))
{
c.setValue(null);
c.setMaxAge(0);
c.setPath("/");
response.addCookie(c);
/*rt= c.getValue();
break;*/
}
}
}
} //往cookie里面 写入值 name 是键 value 是 值
public static void addCookie(HttpServletResponse response, String name,
String value, int maxAge) {
try {
value=URLEncoder.encode(value,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Cookie cookie = new Cookie(name, value);
if (maxAge > 0)
cookie.setMaxAge(maxAge);
cookie.setPath("/");
cookie.setComment("EXPIRING COOKIE at "+ System.currentTimeMillis());
response.addCookie(cookie);
} /**
* 方法描述:只从cookie中获取。如果不存在该cookie,返回null。
* @param request
* @param cookieName
* @return
* @author: Huyihui
* @version: 2012-9-25 上午11:03:04
*/
public static String getCookieOnly(HttpServletRequest request, String cookieName) {
Cookie[] cookieArr = request.getCookies();
if (cookieArr != null && cookieArr.length > 0) {
for (Cookie cookie : cookieArr) {
if (cookie.getName().equals(cookieName)) {
try {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
return null;
} }

笔记

【笔记】cookies管理工具类的更多相关文章

  1. 并发编程学习笔记(10)----并发工具类CyclicBarrier、Semaphore和Exchanger类的使用和原理

    在jdk中,为并发编程提供了CyclicBarrier(栅栏),CountDownLatch(闭锁),Semaphore(信号量),Exchanger(数据交换)等工具类,我们在前面的学习中已经学习并 ...

  2. android的Log日志打印管理工具类(一)

    android的Log日志的打印管理工具类: package com.gzcivil.utils; import android.util.Log; /** * 日志打印管理 * * @author ...

  3. Android 软件管理工具类Utils

    Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...

  4. 阶段3 2.Spring_07.银行转账案例_4 编写事务管理工具类并分析连接和线程解绑

    事务管理工具类 首先需要有connection.并且是当前线程上的connection.声明connectionUtils.提供set方法等着spring来注入 有异常需要放在事务里面 close关闭 ...

  5. iOS核心笔记—源代码管理工具-GIT

    源代码管理工具-GIT 一. git 概述 1. git 简介? 什么是git? > git是一款开源的分布式版本控制工具 > 在世界上所有的分布式版本控制工具中,git是最快.最简单.最 ...

  6. Fragment管理工具类

    Fragment相关→FragmentUtils.java→Demo addFragment : 新增fragment removeFragment : 移除fragment replaceFragm ...

  7. Java学习笔记七——数组工具类Arrays

    数组工具类Arrays Java提供的Arrays类里包含的一些static修饰的方法可以直接操作数组.若将里面的方法用熟的话,那开发效率会大大提高.下面介绍其中的方法. List<T> ...

  8. [Guava学习笔记]Collections: 集合工具类

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3861431.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  9. Dialog对话框管理工具类

    import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; i ...

随机推荐

  1. 安裝 14.04.1 Ubuntu 到 Lenovo thinkpad t460p

    在 Lenovo Thinkpad T460p 安裝 ubuntu, BIOS 需要做一些設定, 沒設定的現象:不斷地停在 usb disk 設定 可以 使用 usb disk install 了!

  2. springmvc请求接收参数的几种方法

    一.通过@PathVariable获取路径中的参数 @RequestMapping(value="user/{id}/{name}",method=RequestMethod.GE ...

  3. Cocos2d-X3.0 刨根问底(四)----- 内存管理源码分析

    本系列文章发表以来得到了很多朋友的关注,小鱼在这里谢谢大家对我的支持,我会继续努力的,最近更新慢了一点,因为我老婆流产了抽了很多时间来照顾她希望大家谅解,并在此预祝我老婆早日康复. 上一篇,我们完整的 ...

  4. TAC Alpha版本 冲冲冲!!!

    第1天 第2天 第3天 第4天 第5天 第6天 第7天 第8天 第9天 第10天 测试随笔 冲刺总结

  5. ruby

    :for 是关键字, each是方法. for 后面的变量,是全局变量,不仅仅存在于for .. end 这个作用域之内 module中的 self.xx方法可以被直接调用 module中的普通方法, ...

  6. 个人学习记录2:ajax跨域封装

    /** * 跨域提交公用方法 * @param param 参数 * @param url 跨域的地址 * @param callFun 回调函数 callFun(data) */ function ...

  7. web前端基础知识-(一)html基本操作

    1. HTML概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样 ...

  8. mysql5.7导入csv文件

    环境: Windows10企业版X64 mysql5.7免安装版(从5.6版本开始,官方不再提供64位的msi版本) 运行mysqld.exe启动mysql进程. 用root登录mysql: mysq ...

  9. foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because 'System.Web.UI.WebControls.Table' does not contain a public definition for 'GetEnumerator'

    错误:foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because ' ...

  10. docker windows 7 mysql安装使用教程

    mysql 1. 镜像的下载 Docker pull MySQL 可以从https://docker.cn/docker/mysql 看到官方的同步镜像. 大约400多M. 2. 运行 每个镜像都有一 ...