Cookie工具类
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder; /**
* Cookie工具类
*/
public class CookieUtil {
/**
* 设置 Cookie(生成时间为1天)
* @param name 名称
* @param value 值
*/
public static void setCookie(HttpServletResponse response, String name, String value) {
setCookie(response, name, value, 60*60*24);
} /**
* 设置 Cookie
* @param name 名称
* @param value 值
*/
public static void setCookie(HttpServletResponse response, String name, String value, String path) {
setCookie(response, name, value, path, 60*60*24);
} /**
* 设置 Cookie
* @param name 名称
* @param value 值
* @param maxAge 生存时间(单位秒)
*/
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
setCookie(response, name, value, "/", maxAge);
} /**
* 设置 Cookie
* @param name 名称
* @param value 值
* @param maxAge 生存时间(单位秒)
*/
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
Cookie cookie = new Cookie(name, null);
cookie.setPath(path);
cookie.setMaxAge(maxAge);
try {
cookie.setValue(URLEncoder.encode(value, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.addCookie(cookie);
} /**
* 获得指定Cookie的值
* @param name 名称
* @return 值
*/
public static String getCookie(HttpServletRequest request, String name) {
return getCookie(request, null, name, false);
}
/**
* 获得指定Cookie的值,并删除。
* @param name 名称
* @return 值
*/
public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) {
return getCookie(request, response, name, true);
}
/**
* 获得指定Cookie的值
* @param request 请求对象
* @param response 响应对象
* @param name 名字
* @param isRemove 是否移除
* @return 值
*/
public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean isRemove) {
String value = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name)) {
try {
value = URLDecoder.decode(cookie.getValue(), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (isRemove) {
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
}
return value;
}
}
IT技术和行业交流群 417691667
Cookie工具类的更多相关文章
- java cookie 工具类
package com.xxx.xxx.xxx.xxx; import java.net.URLDecoder; import java.net.URLEncoder; import javax.se ...
- cookie工具类,解决servlet3.0以前不能添加httpOnly属性的问题
最近在解决XSS注入的问题,由于使用的servlet版本是2.5,不支持httpOnly的属性,故做了个工具类来实现cookie的httpOnly的功能.全类如下: /** * cookie工具类,解 ...
- Cookie工具类 - CookieUtil.java
Cookie工具类,提供Cookie的创建.获取.删除方法. 源码如下:(点击下载 -CookieUtil.java ) import javax.servlet.http.Cookie; impor ...
- Java Cookie工具类
1.Cookie跨域 Cookie不能跨顶级域名访问,但是二级域名可以共享Cookie,所以要实现跨域,有一定的局限性. 2.代码 package com.DingYu.Cookie; import ...
- 180425、cookie工具类
package com.thinkgem.jeesite.common.utils; import java.io.UnsupportedEncodingException; import java. ...
- Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie
Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...
- Cookie 工具类
一.导入 jar 包 <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet ...
- Cookie工具类(获取cookie,设置cookie)
import java.io.UnsupportedEncodingException; import java.net.URLDecoder;import java.net.URLEncoder; ...
- C# Cookie工具类
/// <summary> /// Cookies赋值 /// </summary> /// <param name="strName">主键& ...
随机推荐
- hdu 1370 Biorthythms 中国剩余定理
Biorhythms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- TweenMax参数补充
构造函数:TweenMax(target:Object, duration:Number, vars:Object) target:Object -- 需要缓动的对象 duration:Number ...
- POJ 3415 后缀数组
题目链接:http://poj.org/problem?id=3415 题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数. 思路:首先是两个字符串的问题.所以想用一个'#'把两个字 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- BZOJ3723 : PA2014Final Gra w podwajanie
暴力搜索出所有可行的形状,可以发现本质不同的形状数只有6000个左右. 对于每个形状,它的大小不超过$8\times 8$,故可以按照右下角为原点重建坐标系,用一个unsigned long long ...
- UVa 11324 & 强联通分量+DP
题意: 一张无向图,求点集使其中任意两点可到达. SOL: 强联通分量中的点要么不选要么全都选,然后缩点DAG+DP 记录一下思路,不想写了...代码满天飞.
- UVA 11754 (暴力+中国剩余定理)
题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...
- js的function
1.funciton的js如果直接写是不会执行的 例如 function TableInit() { //var treeNode = $('#otherTree').omTree('getSelec ...
- jquery属性过滤选择器
http://www.jb51.net/article/46279.htm $("div[id]").addClass("highlight"); //查找 ...
- Android & iOS 第三方 Crash ANR 捕捉上传
1. Bugly 地址:http://bugly.qq.com/ 提供:专业的Crash(崩溃).Android ANR(application not response).iOS卡顿监控和解决方案. ...