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

📄 cookieutils.java

📁 模仿当当网基于struts+hierbernate与mysql的商务网站。
💻 JAVA
字号:
package org.whatisjava.dang.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {
	public static final String CART_ID = "dang.cart.id";

	public static final String USER_ID = "dang.user.id";

	public static final String USER_TEMP_ID = "dang.user.tmp";

	/**
	 * 
	 * @param response
	 * @param key
	 * @param value
	 * @param path
	 * @param maxAge
	 */
	public static void addCookie(HttpServletResponse response, String key,
			String value,String path,int maxAge) {
		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(maxAge);
		cookie.setPath(path);
		response.addCookie(cookie);
	}

	/**
	 * @deprecated
	 * @param response
	 * @param key
	 * @param value
	 * @param maxAge
	 */
	public static void addCookie(HttpServletResponse response, String key,
			String value, int maxAge) {
		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(maxAge);
		response.addCookie(cookie);
	}

	/**
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	public static String findCookie(HttpServletRequest request, String key) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (key.equals(cookies[i].getName())) {
					return cookies[i].getValue();
				}
			}
		}
		return null;
	}
}

⌨️ 快捷键说明

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