java-cookie的读写2018-07-23

cookie的读写

importjava.io.UnsupportedEncodingException;

importjava.net.URLDecoder;

importjava.net.URLEncoder;

importjavax.servlet.http.Cookie;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

/**

*

* Cookie 工具类

*

*/

publicfinalclassCookieUtils{

/**

    * 得到Cookie的值, 不编码

    *

*@paramrequest

*@paramcookieName

*@return

    */

publicstaticStringgetCookieValue(HttpServletRequest request, String cookieName){

returngetCookieValue(request, cookieName,false);

    }

/**

    * 得到Cookie的值,

    *

*@paramrequest

*@paramcookieName

*@return

    */

publicstaticStringgetCookieValue(HttpServletRequest request, String cookieName,booleanisDecoder){

        Cookie[] cookieList = request.getCookies();

if(cookieList ==null|| cookieName ==null) {

returnnull;

        }

String retValue =null;

try{

for(inti =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();

        }

returnretValue;

    }

/**

    * 得到Cookie的值,

    *

*@paramrequest

*@paramcookieName

*@return

    */

publicstaticStringgetCookieValue(HttpServletRequest request, String cookieName, String encodeString){

        Cookie[] cookieList = request.getCookies();

if(cookieList ==null|| cookieName ==null) {

returnnull;

        }

String retValue =null;

try{

for(inti =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的值 不设置生效时间默认浏览器关闭即失效,也不编码

    */

publicstaticvoidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

            String cookieValue){

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

    }

/**

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

    */

publicstaticvoidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,intcookieMaxage){

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

    }

/**

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

    */

publicstaticvoidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,booleanisEncode){

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

    }

/**

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

    */

publicstaticvoidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,intcookieMaxage,booleanisEncode){

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

    }

/**

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

    */

publicstaticvoidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,

String cookieValue,intcookieMaxage, String encodeString){

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

    }

/**

    * 删除Cookie带cookie域名

    */

publicstaticvoiddeleteCookie(HttpServletRequest request, HttpServletResponse response,

            String cookieName){

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

    }

/**

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

    *

*@paramcookieMaxage cookie生效的最大秒数

    */

privatestaticfinalvoiddoSetCookie(HttpServletRequest request, HttpServletResponse response,

String cookieName, String cookieValue,intcookieMaxage,booleanisEncode){

try{

if(cookieValue ==null) {

cookieValue ="";

}elseif(isEncode) {

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

            }

Cookie cookie =newCookie(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的值,并使其在指定时间内生效

    *

*@paramcookieMaxage cookie生效的最大秒数

    */

privatestaticfinalvoiddoSetCookie(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);

            System.out.println(domainName);

if(!"localhost".equals(domainName)) {

                cookie.setDomain(domainName);

                }

            }

cookie.setPath("/");

            response.addCookie(cookie);

}catch(Exception e) {

        e.printStackTrace();

        }

    }

/**

    * 得到cookie的域名

    */

privatestaticfinalStringgetDomainName(HttpServletRequest request){

String domainName =null;

        String serverName = request.getRequestURL().toString();

if(serverName ==null|| serverName.equals("")) {

domainName ="";

}else{

            serverName = serverName.toLowerCase();

serverName = serverName.substring(7);

finalintend = serverName.indexOf("/");

serverName = serverName.substring(0, end);

finalString[] domains = serverName.split("\\.");

intlen = domains.length;

if(len >3) {

// www.xxx.com.cn

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

}elseif(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的读写2018-07-23)