In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "java tool class CookieUtils usage analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Package com.hjj.demo.commons.utils;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 utility class *
Title: CookieUtils
*
Description:
* * @ author hjj * @ version 1.0.0 * @ date 22:00 on 2019-10-7 * / public final class CookieUtils {/ * get the value of Cookie without encoding * * @ param request * @ param cookieName * @ return * / public static String getCookieValue (HttpServletRequest request, String cookieName) {return getCookieValue (request, cookieName, false) } / * get the value of Cookie, * * @ param request * @ param cookieName * @ return * / public static String getCookieValue (HttpServletRequest request, String cookieName, boolean isDecoder) {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)) { if (isDecoder) { retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8"); } else { retValue = cookieList[i].getValue(); } break; } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return retValue; } /** * 得到Cookie的值, * * @param request * @param cookieName * @return */ public static String 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(); } return retValue; } /** * 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue) { setCookie(request, response, cookieName, cookieValue, -1); } /** * 设置Cookie的值 在指定时间内生效,但不编码 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage) { setCookie(request, response, cookieName, cookieValue, cookieMaxage, false); } /** * 设置Cookie的值 不设置生效时间,但编码 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, boolean isEncode) { setCookie(request, response, cookieName, cookieValue, -1, isEncode); } /** * 设置Cookie的值 在指定时间内生效, 编码参数 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) { doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode); } /** * 设置Cookie的值 在指定时间内生效, 编码参数(指定编码) */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, String encodeString) { doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString); } /** * 删除Cookie带cookie域名 */ public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) { doSetCookie(request, response, cookieName, "", -1, false); } /** * 设置Cookie的值,并使其在指定时间内生效 * * @param cookieMaxage cookie生效的最大秒数 */ private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) { try { if (cookieValue == null) { cookieValue = ""; } else if (isEncode) { cookieValue = URLEncoder.encode(cookieValue, "utf-8"); } Cookie cookie = new Cookie(cookieName, cookieValue); if (cookieMaxage >0) cookie.setMaxAge (cookieMaxage); if (null! = request) {/ / set the domain name 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 () }} / * * set the value of Cookie And make it effective within a specified time * / private static final void doSetCookie (HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, String encodeString) {try {if (cookieValue = = null) {cookieValue = "" } else {cookieValue = URLEncoder.encode (cookieValue, encodeString);} Cookie cookie = new Cookie (cookieName, cookieValue); if (cookieMaxage > 0) cookie.setMaxAge (cookieMaxage); if (null! = request) {/ / set cookie String domainName = getDomainName (request) of the domain name / / System.out.println (domainName); if (! "localhost" .equals (domainName)) {cookie.setDomain (domainName);}} cookie.setPath ("/"); response.addCookie (cookie);} catch (Exception e) {e.printStackTrace () }} / * get the domain name of cookie * / private static final String getDomainName (HttpServletRequest request) {String domainName = null; String serverName = request.getRequestURL (). ToString (); if (serverName = = null | | serverName.equals (")) {domainName =";} else {serverName = serverName.toLowerCase () ServerName = serverName.substring (7); 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 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];} return domainName;}} "CookieUtils usage Analysis of java tools" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.