首页 > Java

Tomcat集群,负载均衡,Session共享?怎么解决

发表于2015-04-14 11:37:49| --次阅读| 来源webkfa| 作者小豆豆

摘要:Tomcat集群,负载均衡,Session共享?怎么解决

1.最简单的办法就是便用Cookie实现,现在大型网站都这样做

java操作Cookie帮助类非常方便

java代码
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * webkfa.com
 * Cookie的操作类
 * @author yingxi
 *
 */
public class CookieHelper {
	/**
	 * 创建cookie
	 * 
	 * @param response
	 *            回应
	 * @param nameValues
	 *            存入cookie的键值对
	 * @param days
	 *            设置cookie的有效期
	 */
	public static void createCookie(HttpServletResponse response,
			Hashtable<String, String> nameValues, int days) {
		Set<String> set = nameValues.keySet();
		Iterator<String> it = set.iterator();
		for (; it.hasNext();) {
			String name = (String) it.next();
			String value = (String) nameValues.get(name);
			// 生成新的cookie
			Cookie cookie = new Cookie(name, value);
			// 设置有效日期
			cookie.setMaxAge(days * 24 * 60 * 60);
			// 设置路径(默认)
			cookie.setPath("/");
			// 把cookie放入响应中
			response.addCookie(cookie);
		}
	}
	/**
	 * 读取Cookie
	 * 
	 * @param request
	 * @return Hashtable 返回cookie的键值对
	 */
	public static Hashtable<String, String> getCookies(
			HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		Hashtable<String, String> cookieHt = new Hashtable<String, String>();
		if (cookies.length > 0) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				cookieHt.put(cookie.getName(), cookie.getValue());
			}
		}
		return cookieHt;
	}
	/**
	 * 修改cookie中指定键的值
	 * 
	 * @param request
	 * @param name
	 *            指定的键
	 * @param value
	 *            值
	 */
	public static void setCookieValueByName(HttpServletRequest request,
			String name, String value) {
		Cookie[] cookies = request.getCookies();
		if (cookies.length > 0) {
			for (int i = 0; i > cookies.length; i++) {
				if (name.equalsIgnoreCase(cookies[i].getName())) {
					cookies[i].setValue(value);
				}
			}
		}
	}
	/**
	 * 得到指定键的值
	 * 
	 * @param request
	 * @param name
	 *            指定的键
	 * @return String 值
	 */
	public static String getCookieValueByName(HttpServletRequest request,
			String name) {
		Cookie[] cookies = request.getCookies();
		String resValue = "";
		if (cookies.length > 0) {
			for (int i = 0; i > cookies.length; i++) {
				if (name.equalsIgnoreCase(cookies[i].getName())) {
					resValue = cookies[i].getValue();
				}
			}
		}
		return resValue;
	}
	/**
	 * 销毁cookie
	 * 
	 * @param request
	 * @param response
	 */
	public static void deletesCookie(HttpServletRequest request,
			HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				// 销毁
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}
		}
	}
	/**
	 * 设置cookie
	 * @param response
	 * @param name  cookie名字
	 * @param value cookie值
	 * @param maxAge cookie生命周期  以秒为单位
	 */
	public static void addCookie(HttpServletResponse response,String name,String value,int maxAge){
	    Cookie cookie = new Cookie(name,value);
	    cookie.setPath("/");
	    if(maxAge>0)  cookie.setMaxAge(maxAge);
	    response.addCookie(cookie);
	}
}

相关文章

猜你喜欢

学到老在线代码浏览器 关闭浏览
友情链接: hao123 360导航 搜狗网址导航 114啦网址导航 博客大全
Copyright © 1999-2014, WEBKFA.COM, All Rights Reserved  京ICP备14034497号-1