⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cookieutils.java

📁 这个是网络上下载的一个struct框架的程序
💻 JAVA
字号:
/** * $RCSfile: CookieUtils.java,v $ * $Revision: 1.5 $ * $Date: 2003/02/21 07:09:12 $ * * Copyright (C) 2002 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.struts2.framework.util;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * A set of methods for manipulating cookies. * * @see javax.servlet.http.Cookie * @see javax.servlet.http.HttpServletRequest#getCookies * @author Bill Lynch * @author Bruce Ritchie */public class CookieUtils {    /**     * Returns the specified cookie, or <tt>null</tt> if the cookie     * does not exist. Note: because of the way that cookies are implemented     * it's possible for multiple cookies with the same name to exist (but with     * different domain values). This method will return the first cookie that     * has a name match.     *     * @param request a HttpServletRequest object.     * @param name the name of the cookie.     * @return the Cookie object if it exists, otherwise <tt>null</tt>.     */    public static Cookie getCookie(HttpServletRequest request, String name) {        Cookie cookies[] = request.getCookies();        // Return null if there are no cookies or the name is invalid.        if (cookies == null || name == null || name.length() == 0) {            return null;        }        // Otherwise, we  do a linear scan for the cookie.        Cookie cookie = null;        for (int i = 0; i < cookies.length; i++) {            // If the current cookie name matches the one we're looking for, we've            // found a matching cookie.            if (cookies[i].getName().equals(name)) {                cookie = cookies[i];                // The best matching cookie will be the one that has the correct                // domain name. If we've found the cookie with the correct domain name,                // return it. Otherwise, we'll keep looking for a better match.                if (request.getServerName().equals(cookie.getDomain())) {                    break;                }            }        }        return cookie;    }    /**     * Deletes the specified cookie.     *     * @param response a HttpServletResponse object.     * @param cookie the cookie object to be deleted.     */    public static void deleteCookie(HttpServletResponse response, Cookie cookie) {        if (cookie != null) {            // Invalidate the cookie            cookie.setPath("/");            cookie.setValue("");            cookie.setMaxAge(0);            response.addCookie(cookie);        }    }    /**     * Stores a value in a cookie. By default this cookie will persist for 30 days.     *     * @see #setCookie(HttpServletResponse, String, String, int)     * @param response a HttpServletResponse object.     * @param name a name to identify the cookie.     * @param value the value to store in the cookie.     */    public static void setCookie(HttpServletResponse response, String name, String value) {        // Save the cookie value for 1 month        setCookie(response, name, value, 60*60*24*30);    }    /**     * Stores a value in a cookie. This cookie will persist for the amount     * specified in the <tt>saveTime</tt> parameter.     *     * @see #setCookie(HttpServletResponse, String, String)     * @param response a HttpServletResponse object.     * @param name a name to identify the cookie.     * @param value the value to store in the cookie.     * @param maxAge the time (in seconds) this cookie should live.     */    public static void setCookie(HttpServletResponse response, String name, String value,            int maxAge)    {        // Check to make sure the new value is not null (appservers like Tomcat        // 4 blow up if the value is null).        if (value == null) {            value = "";        }        Cookie cookie = new Cookie(name, value);        cookie.setMaxAge(maxAge);        cookie.setPath("/");        response.addCookie(cookie);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -