博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cookie保存信息
阅读量:5077 次
发布时间:2019-06-12

本文共 6931 字,大约阅读时间需要 23 分钟。

利用cookie作为保存信息,需要做处理的是获取domain,特别是跨域问题,不同的网址域名不一样,

package com.rb.utils.auth;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 final class CookieUtils {    /**     * 得到Cookie的值, 不编码     *      * @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的域名 */ public static final String getDomainName(HttpServletRequest request) { String url = request.getRequestURL().toString(); String result = ""; int j = 0, startIndex = 0, endIndex = 0; for (int i = 0; i < url.length(); i++) { if (url.charAt(i) == '/') { j++; if (j == 2) startIndex = i; else if (j == 3) endIndex = i; } } result = url.substring(startIndex + 1, endIndex); if (result != null && result.indexOf(":") > 0) { String[] ary = result.split("\\:"); result = ary[0]; } return result; }}

 

根据请求动态获取域名。

转载于:https://www.cnblogs.com/lazyli/p/11062484.html

你可能感兴趣的文章
Java8 Lambda表达应用 -- 单线程游戏server+异步数据库操作
查看>>
AngularJS学习篇(一)
查看>>
关于Xshell无法连接centos6.4的问题
查看>>
spring security 11种过滤器介绍
查看>>
代码实现导航栏分割线
查看>>
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
luogu4849 寻找宝藏 (cdq分治+dp)
查看>>
关于源程序到可运行程序的过程
查看>>
C# Async与Await的使用
查看>>
Mysql性能调优
查看>>
iOS基础-UIKit框架-多控制器管理-实例:qq界面框架
查看>>
自定义tabbar(纯代码)
查看>>
小程序底部导航栏
查看>>
poj1611 简单并查集
查看>>
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>
WPF星空效果
查看>>
WPF Layout 系统概述——Arrange
查看>>
PIGOSS
查看>>