170403、java 版cookie操作工具类
- package com.rick.utils;
- 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;
- /**
- *
- * -------------------------------------------
- * Title : CookieUtils
- * Description : Cookie 工具类
- * Create on : 2017年4月03日 上午11:12:52
- * Copyright (C) strongunion
- * @author RICK
- * 修改历史:
- * 修改人 修改日期 修改描述
- * -------------------------------------------
- */
- public final class CookieUtil {
- /**
- * 得到Cookie的值, 不编码
- * @author RICK
- * @param request
- * @param cookieName
- * @return
- */
- public static String getCookieValue(HttpServletRequest request, String cookieName) {
- return getCookieValue(request, cookieName, false);
- }
- /**
- * 得到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) {// 设置域名的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的值,并使其在指定时间内生效
- *
- * @param cookieMaxage cookie生效的最大秒数
- */
- 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) {// 设置域名的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的域名
- */
- 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 <= 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];
- }
- return domainName;
- }
- }
170403、java 版cookie操作工具类的更多相关文章
- 170404、java版ftp操作工具类
package com.rick.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...
- Cookie 操作工具类
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet ...
- java/javascript 时间操作工具类
一.java 时间操作工具类 import org.springframework.util.StringUtils; import java.text.ParseException; import ...
- java:数组操作工具类 java.util.Arrays包 主要方法详解
Arrays类位于Java.util包下,是一个对数组操作的工具类,现将Arrays类中的方法做一个总结(JDK版本:1.6.0_34).Arrays类中的方法可以分为八类: sort(对数组排序) ...
- Java版InfluxDB工具类
InfluxDB工具类 package com.influxdb.test; import java.util.Map; import org.influxdb.InfluxDB; import or ...
- JAVA 文本 TXT 操作工具类
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...
- 170405、java版MD5工具类
package com.rick.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...
- Java IO(文件操作工具类)
FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...
- java版RSA工具类
/** * RSA算法加密/解密工具类 */ public class RSAUtils { private static final Logger LOGGER = LoggerFactory.ge ...
随机推荐
- vue使用axios,进行网络请求
1.首先自己创建一个组件: https://www.cnblogs.com/fps2tao/p/9559291.html 2.安装:axios(可以npm安装,也可以下载js引入文件) npm ins ...
- 【Android】14.0 第14章 内部存储与外部SD卡存储—本章示例主界面
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 Android使用的文件系统是基于Linux的文件系统,在Android应用程序中,开发人员既可以建立和访问程序自 ...
- Android从无知到有知——NO.5
今天整一下利用广播实现ip拨号. 这一块主要用到的知识是android四大组件之中的一个的broadcast receiver(广播接收者).那么它接收什么东东呢,就是我们所无谓的一个个的事件,比 ...
- Node.js用fs.renameSync报cross-device link not permitted错
转自: http://blog.csdn.net/starrexstar/article/details/8048722 今天把 Manuel Kiessling 的[The Node Beginne ...
- dubbo异步调用三种方式
异步通讯对于服务端响应时间较长的方法是必须的,能够有效地利用客户端的资源,在dubbo中,消费端<dubbp:method>通过 async="true"标识. < ...
- [转]__cdecl与__stdcall
来自Programming Windows 5th Edition The WinMain function is given a type of WINAPI (as is every Window ...
- Android中<uses-sdk>属性和target属性分析
1. 概要 <uses-sdk> 用来描述该应用程序可以运行的最小和最大API级别,以及应用程序开发者设计期望运行的平台版本.通过在manifest清单文件中添加该属性,我们可以更好的控制 ...
- 继承log4.net的类
using System; using System.Diagnostics; [assembly: log4net.Config.XmlConfigurator(Watch = true)] nam ...
- 使用info命令查看Redis信息和状态
redis-cli连接服务器后,使用info命令查看Redis信息和状态: ? 1 info 其中memory段显示了redis的内存使用状态. 以下内容复制自:http://redisdoc.com ...
- 用javascript技术读取注册表中软件安装位置并启动本地软件
1.首先读取注册表中本地软件安装的位置,如果未安装则无就跳转到下载页面. 2.启动软件,关闭页面. 3.如报错提示. <SCRIPT language=javascript> <! ...