我是靠谱客的博主 会撒娇饼干,最近开发中收集的这篇文章主要介绍java cookie包_Java Cookie工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

packagecom.DingYu.Cookie;importjava.io.UnsupportedEncodingException;importjava.net.URLDecoder;importjava.net.URLEncoder;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/***

* Cookie 工具类

**/

public final classCookieUtils {/*** 得到Cookie的值, 不编码

*

*@paramrequest

*@paramcookieName

*@return

*/

public staticString getCookieValue(HttpServletRequest request, String cookieName) {return getCookieValue(request, cookieName, false);

}/*** 得到Cookie的值,

*

*@paramrequest

*@paramcookieName

*@return

*/

public static String getCookieValue(HttpServletRequest request, String cookieName, booleanisDecoder) {//获得所有的Cookie

Cookie[] cookieList =request.getCookies();if (cookieList == null || cookieName == null) {return null;

}

String retValue= null;try{for (int i = 0; i < cookieList.length; i++) {if(cookieList[i].getName().equals(cookieName)) {//如果true 使用UTF-8 进行解码

if(isDecoder) {

retValue= URLDecoder.decode(cookieList[i].getValue(), "UTF-8");

}else{

retValue=cookieList[i].getValue();

}break;

}

}

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}returnretValue;

}/*** 得到Cookie的值,使用自定义的编码类型进行解码

*

*@paramrequest

*@paramcookieName

*@return

*/

public staticString getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {

Cookie[] cookieList=request.getCookies();if (cookieList == null || cookieName == null) {return null;

}

String retValue= null;try{for (int i = 0; i < cookieList.length; i++) {if(cookieList[i].getName().equals(cookieName)) {//使用自定义的编码类型进行解码

retValue =URLDecoder.decode(cookieList[i].getValue(), encodeString);break;

}

}

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}returnretValue;

}/*** 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码*/

public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue) {

setCookie(request, response, cookieName, cookieValue,-1);

}/*** 设置Cookie的值 在指定时间内生效,但不编码*/

public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,intcookieMaxage) {

setCookie(request, response, cookieName, cookieValue, cookieMaxage,false);

}/*** 设置Cookie的值 不设置生效时间,但编码*/

public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,booleanisEncode) {

setCookie(request, response, cookieName, cookieValue,-1, isEncode);

}/*** 设置Cookie的值 在指定时间内生效, 编码参数*/

public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,int cookieMaxage, booleanisEncode) {

doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);

}/*** 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)*/

public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,intcookieMaxage, String encodeString) {

doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);

}/*** 删除Cookie带cookie域名,删除原理,把原来的cookie的Value用""*/

public static voiddeleteCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName) {

doSetCookie(request, response, cookieName,"", -1, false);

}/*** 设置Cookie的值,并使其在指定时间内生效

*

*@paramcookieMaxage cookie生效的最大秒数*/

private static final voiddoSetCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName, String cookieValue,int cookieMaxage, booleanisEncode) {try{if (cookieValue == null) {

cookieValue= "";

}else if(isEncode) {//使用UTF-8进行编码

cookieValue = URLEncoder.encode(cookieValue, "utf-8");

}

Cookie cookie= newCookie(cookieName, cookieValue);if (cookieMaxage > 0)//设置Cookie的生存时间

cookie.setMaxAge(cookieMaxage);if (null != request) {//设置域名的cookie

String domainName =getDomainName(request);

System.out.println(domainName);if (!"localhost".equals(domainName)) {

cookie.setDomain(domainName);

}

}

cookie.setPath("/");

response.addCookie(cookie);

}catch(Exception e) {

e.printStackTrace();

}

}/*** 设置Cookie的值,并使其在指定时间内生效

*

*@paramcookieMaxage cookie生效的最大秒数*/

private static final voiddoSetCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName, String cookieValue,intcookieMaxage, String encodeString) {try{if (cookieValue == null) {

cookieValue= "";

}else{

cookieValue=URLEncoder.encode(cookieValue, encodeString);

}

Cookie cookie= newCookie(cookieName, cookieValue);if (cookieMaxage > 0)

cookie.setMaxAge(cookieMaxage);if (null != request) {//设置域名的cookie

String domainName =getDomainName(request);if (!"localhost".equals(domainName)) {

cookie.setDomain(domainName);

}

}

cookie.setPath("/");

response.addCookie(cookie);

}catch(Exception e) {

e.printStackTrace();

}

}/*** 得到cookie的域名*/

private static finalString getDomainName(HttpServletRequest request) {

String domainName= null;//获得请求的域名

String serverName =request.getRequestURL().toString();if (serverName == null || serverName.equals("")) {

domainName= "";

}else{//全转为小写

serverName =serverName.toLowerCase();//从第七个开始截取,过滤掉http:// serverName = serverName.substring(7);//到后面的参数为止 比如 www.taobao.com/name=xxx

final int end = serverName.indexOf("/");//把参数截取

serverName = serverName.substring(0, end);//根据点来截取

final String[] domains = serverName.split("\.");int len =domains.length;if (len > 3) {//www.xxx.com.cn

domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];

}else if (len <= 3 && len > 1) {//xxx.com or xxx.cn

domainName = "." + domains[len - 2] + "." + domains[len - 1];

}else{

domainName=serverName;

}

}//去掉端口号

if (domainName != null && domainName.indexOf(":") > 0) {

String[] ary= domainName.split("\:");

domainName= ary[0];

}returndomainName;

}

}

最后

以上就是会撒娇饼干为你收集整理的java cookie包_Java Cookie工具类的全部内容,希望文章能够帮你解决java cookie包_Java Cookie工具类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部