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

📄 cookieutils.java

📁 JSP设计(第三版)一书源代码 JSP设计(第三版)》得到了充分的修订和更新
💻 JAVA
字号:
package com.ora.jsp.util;

import javax.servlet.http.*;

/**
 * This class contains a number of static methods that can be used to
 * work with javax.servlet.Cookie objects.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 2.0
 */
public class CookieUtils {
    
    /**
     * Returns the value of the Cookie with the specified name,
     * or null if not found.
     */
    public static String getCookieValue(String name, HttpServletRequest req) {
        Cookie[] cookies = req.getCookies();
	if (cookies == null) {
	    return null;
	}

        String value = null;
	for (int i = 0; i < cookies.length; i++) {
	    if (cookies[i].getName().equals(name)) {
		value = cookies[i].getValue();
		break;
	    }
	}
        return value;
    }
    
    /**
     * Creates a Cookie with the specified name, value and max age,
     * and adds it to the response.
     */
    public static void sendCookie(String name, String value, int maxAge,
        HttpServletResponse res) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        res.addCookie(cookie);
    }
 
    /**
     * Returns true if a cookie with the specified name is
     * present in the request.
     */
    public static boolean isCookieSet(String name, HttpServletRequest req) {
        return getCookieValue(name, req) != null;
    }
}

⌨️ 快捷键说明

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