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. URL类

    java.net.URL类是对统一资源定位符(如http://www.lolcats.com)的抽象.它扩展了java.lang.Object,是一个final类.它采用策略模式,协议处理器(prot ...

  2. Git从入门到学会

    Git简介 Git是什么? Git和SVN一样都是一种高效的管理代码的系统. Git是目前世界上最先进的分布式版本控制系统(没有之一). 创建版本库 什么是版本库呢?版本库又名仓库,英文名reposi ...

  3. Django Channels 学习笔记

    一.为什么要使用Channels 在Django中,默认使用的是HTTP通信,不过这种通信方式有个很大的缺陷,就是不能很好的支持实时通信.如果硬是要使用HTTP做实时通信的话只能在客户端进行轮询了,不 ...

  4. Java数组及其内存分配

    几乎所有的程序设计语言都支持数组.Java也不例外.当我们需要多个类型相同的变量的时候,就考虑定义一个数组.在Java中,数组变量是引用类型的变量,同时因为Java是典型的静态语言,因此它的数组也是静 ...

  5. c# 程序设计教程笔记

    值类型:[简单类型[整数类型(sbyte,byte,short,ushort,int uint,long....),字符类型),布尔类型,实数类型],结构类型, 枚举类型]. 引用类型:[类,委托,数 ...

  6. mate-desktop安装在其他目录时一些配置信息

    1.mate-desktop安装在其他目录时一些配置信息 2.BIN目录下添加相应的mate-session_gtk2/3 3.首先配置如下环境变量 #!/bin/sh if [[ "${E ...

  7. 纯js+css实现loading等待效果

    此插件是基于jqueryUI的widget,下面是具体实现代码 第一部分css: /***loading***/ .loading-box{ position:absolute; text-align ...

  8. 第二轮冲刺-Runner站立会议08

    今天:优化日历界面 明天:将日历界面与主程序结合

  9. 修改Excel2013默认模版(启动模版和新建Sheet模版)

    1.  C:\Windows\ShellNew\EXCEL12.XLSX 设置好格式后另存为, 然后复制过来覆盖掉,如果覆盖不了,注意修改所有者权限 2. 新建文件保存为模版文件Sheet.xltx, ...

  10. 百度编辑器 ueditor 内容编辑自动套P标签,及p标签 替换

    如图,红框为回车键和shift+回车 :    ===>>  ueditor.all.js中: 1: 搜索修改成false:allowDivTransToP: false 再搜索并修改以下 ...