cookie工具
来源:互联网 发布:淘宝搜索暗语大全 编辑:程序博客网 时间:2024/05/02 02:51
package com.other.helper;
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 org.apache.commons.lang.StringUtils;
import com.other.PublicDefine;
/**
* cookie常用操作工具类
* @author Jackie
* @since 2015-08-10
* @version 1.0.0
*/
public class CookieUtil {
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge, String path, String domain, Boolean secure) {
try {
name = URLEncoder.encode(name, "UTF-8");
value = URLEncoder.encode(value, "UTF-8");
Cookie cookie = new Cookie(name, value);
if (maxAge != null) {
cookie.setMaxAge(maxAge.intValue());
}
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
if (secure != null) {
cookie.setSecure(secure.booleanValue());
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge) {
addCookie(request, response, name, value, maxAge, "/", null, null);
}
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
addCookie(request, response, name, value, null, null, null, null);
}
public static String getCookie(HttpServletRequest request, String name) {
Cookie[] arrayCookie = request.getCookies();
if (arrayCookie != null) {
try {
name = URLEncoder.encode(name, "UTF-8");
for (Cookie cookie : arrayCookie) {
if (name.equals(cookie.getName())) {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
}
}
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}
return null;
}
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, String path,
String domain) {
try {
name = URLEncoder.encode(name, "UTF-8");
Cookie cookie = new Cookie(name, null);
cookie.setMaxAge(0);
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
// removeCookie(request, response, name, localSetting.getCookiePath(),
// localSetting.getCookieDomain());
removeCookie(request, response, name, "/", null);
}
//xiaolan version5 add
public static void locationRequest(HttpServletRequest request, HttpServletResponse response,String py,String name){
if(StringUtils.isNotEmpty(py)){
CookieUtil.addCookie(request, response, PublicDefine.DEFALUT_LOCALHOST_ID, py+":"+name, PublicDefine.DEFAULT_COOKIE_LIFECYCLE);
}
}
//xiaolan version5 add
public static String GetLocationCity(HttpServletRequest request) {
String location = CookieUtil.getCookie(request, PublicDefine.DEFALUT_LOCALHOST_ID);
if( location != null && location != "" )
{
String [] list = location.split(":");
if (StringUtils.isNotEmpty(location) && (list.length == 2)) {
return list[0];
} else {
return "";
}
}
else
return "";
}
}
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 org.apache.commons.lang.StringUtils;
import com.other.PublicDefine;
/**
* cookie常用操作工具类
* @author Jackie
* @since 2015-08-10
* @version 1.0.0
*/
public class CookieUtil {
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge, String path, String domain, Boolean secure) {
try {
name = URLEncoder.encode(name, "UTF-8");
value = URLEncoder.encode(value, "UTF-8");
Cookie cookie = new Cookie(name, value);
if (maxAge != null) {
cookie.setMaxAge(maxAge.intValue());
}
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
if (secure != null) {
cookie.setSecure(secure.booleanValue());
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge) {
addCookie(request, response, name, value, maxAge, "/", null, null);
}
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
addCookie(request, response, name, value, null, null, null, null);
}
public static String getCookie(HttpServletRequest request, String name) {
Cookie[] arrayCookie = request.getCookies();
if (arrayCookie != null) {
try {
name = URLEncoder.encode(name, "UTF-8");
for (Cookie cookie : arrayCookie) {
if (name.equals(cookie.getName())) {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
}
}
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}
return null;
}
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, String path,
String domain) {
try {
name = URLEncoder.encode(name, "UTF-8");
Cookie cookie = new Cookie(name, null);
cookie.setMaxAge(0);
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
}
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
// removeCookie(request, response, name, localSetting.getCookiePath(),
// localSetting.getCookieDomain());
removeCookie(request, response, name, "/", null);
}
//xiaolan version5 add
public static void locationRequest(HttpServletRequest request, HttpServletResponse response,String py,String name){
if(StringUtils.isNotEmpty(py)){
CookieUtil.addCookie(request, response, PublicDefine.DEFALUT_LOCALHOST_ID, py+":"+name, PublicDefine.DEFAULT_COOKIE_LIFECYCLE);
}
}
//xiaolan version5 add
public static String GetLocationCity(HttpServletRequest request) {
String location = CookieUtil.getCookie(request, PublicDefine.DEFALUT_LOCALHOST_ID);
if( location != null && location != "" )
{
String [] list = location.split(":");
if (StringUtils.isNotEmpty(location) && (list.length == 2)) {
return list[0];
} else {
return "";
}
}
else
return "";
}
}
0 0
- cookie工具
- 工具类:Cookie 工具类
- cookie工具类
- JavaScript Cookie 工具类
- cookie 工具类
- Cookie工具类
- Cookie工具类
- cookie工具类
- cookie 工具类
- Cookie工具类
- cookie工具类
- Cookie工具类
- Cookie工具类
- Cookie 工具类
- 关于cookie的操作-----Cookie工具类
- 关于cookie的操作-----Cookie工具类
- Cookie测试工具小汇
- Cookie测试工具小汇
- vs2012窗口布局恢复
- ComponentName的应用
- 【箭扣观点】工具App如何活到下一个春天?
- Redis学习笔记5--Redis排序
- 支付宝SDK导入
- cookie工具
- UrlRewrite的简单实用
- Hive配置与操作实践
- 移动端自适应rem
- 转:VS2012中将当前选定项目做为启动项
- sql server 镜像强制服务
- .Net中并行计算的了解
- 深度学习(二十一)基于FCN的图像语义分割-CVPR 2015-未完待续
- 多线程 1