Apache.Tomcat整合,用cookie取代Tomcat之间Session的复制

公司网站用Apache+Tomcat集群后,经过观察发现Tomcat之间的Session复制非常的消耗资源。一个Tomcat挂掉后,另外一个要复制很久才能复制完成。导致如果session很多,一个Tomcat挂掉,网站访问变得很慢.

现在改成Cookie来取替Tomcat之间的复制,具体实现方式如下:

 

登录时将用户信息存入一份在Session中,然后向用户的本机中插入一条cookie信息。由于去掉了Tomcat之间Session的复制所以需要用到Session业务的时候,在一个Tomcat中有session信息,如果在这个过程中被分配到另外一个Tomcat运行后就会出现找不到Session信息的错误。考虑到这一条我写了一个过滤器来对网站的请求进行过滤,先判断session中有没有值。如果有就过,如果没有就到本地来取一次cookie,如果存在即在当前Tomcat上恢复Session.代码如下

 

Cookie工具类

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

/**
 * Cookie 工具类增,删,查
 * 
 * @author ws715
 * 
 */
public class CookieUtil {
	/**
	 * 按名字得到Cookie
	 * 
	 * @param request
	 * @param name
	 * @return
	 */

	private CookieUtil() {
	}

	public static Cookie getCookie(HttpServletRequest request, String name) {
		Cookie cookies[] = request.getCookies();
		if (cookies == null || name == null || name.length() == 0) {
			return null;
		}
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (name.equals(cookies[i].getName())) {
					return cookies[i];
				}
			}
		}
		return null;
	}

	/**
	 * 将cookie中的数据保存到session中
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static boolean setSessionFormCookie(HttpServletRequest request,
			String name) {

		String target = null;
		Cookie cookies[] = request.getCookies();
		boolean bool = false;
		if (cookies == null || name == null || name.length() == 0) {
			bool = false;
		}

		if (cookies != null) {

			for (int i = 0; i < cookies.length; i++) {

				if (name.equals(cookies[i].getName())) {
					target = cookies[i].getValue();
					break;
				}
			}

		}

		if (target != null && !target.equals("")) {

			HttpSession session = request.getSession();
			session.setAttribute("UserName", target.toString());
			bool = true;
		}
		return bool;
	}

	/**
	 * 删除Cookie
	 * 
	 * @param request
	 * @param response
	 * @param cookie
	 */
	public static void deleteCookie(HttpServletRequest request,
			HttpServletResponse response, Cookie cookie) {
		if (cookie != null) {
			cookie.setPath(getPath(request));
			cookie.setValue("");
			// cookie.setDomain("");
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}

	/**
	 * 按名字删除
	 * 
	 * @param request
	 * @param response
	 * @param name
	 */
	public static void deleteCookie(HttpServletRequest request,
			HttpServletResponse response, String name) {

		Cookie cookies[] = request.getCookies();
		Cookie myCookie = null;
		boolean bool = false;
		if (cookies == null || name == null || name.length() == 0) {
			throw new NullPointerException(
					"getCookie deleteCookie method name is not null");
		}
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (name.equals(cookies[i].getName())) {
					myCookie = cookies[i];
					break;
				}
			}

			if (myCookie != null) {
				deleteCookie(request, response, myCookie);
			}
		}
	}

	/**
	 * 保存到Cookie中
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletRequest request,
			HttpServletResponse response, String name, String value) {
		setCookie(request, response, name, value, 0x278d00);
	}

	/**
	 * 可以设置时间
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param value
	 * @param maxAge
	 */
	public static void setCookie(HttpServletRequest request,
			HttpServletResponse response, String name, String value, int maxAge) {
		Cookie cookie = new Cookie(name, value == null ? "" : value);
		cookie.setMaxAge(maxAge);
		// cookie.setDomain(request.getServerName());
		cookie.setPath(getPath(request));
		response.addCookie(cookie);
	}

	private static String getPath(HttpServletRequest request) {
		String path = request.getContextPath();
		return (path == null || path.length() == 0) ? "/" : path;
	}

	/**
	 * 从cookie中获得username
	 * 
	 * @param request
	 * @param name
	 * @return
	 */

	public static String getUserNameForCookie(HttpServletRequest request,
			String name) {
		String username = "";
		Cookie cookies[] = request.getCookies();
		if (cookies == null || name == null || name.length() == 0) {
			return null;
		}

		if (cookies != null) {

			for (int i = 0; i < cookies.length; i++) {

				if (name.equals(cookies[i].getName())) {
					try {
						username = (cookies[i].getValue().split("#"))[0];
					} catch (Exception e) {
						username = null;
					}
				}
			}

		}
		return username;
	}

}

 

过滤器

 

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionCookieFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
	        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
	        // 通过检查session中的变量,过虑请求
	        HttpSession session = httpServletRequest.getSession();
	        
	        String   UserName="guest" ;
	        
	 
	        if(session.getAttribute("UserName")==null || 
session.getAttribute("UserName").equals("guest")){ 
	        
	         if(!com.pixel.util.CookieUtil.setSessionFormCookie(httpServletRequest,"cookiename")){  
	        	   session.setAttribute("UserName",UserName);
	        }
	        }
	        
	        chain.doFilter(request, response);  
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}

}

 

登录时加入cookie中

 

						 CookieUtil.setCookie(request,response,"cookiename",cookieValue.toString(),60*120);     
 

不知道还有没有更好的办法和改进的余地

源码下载地址: https://pan.quark.cn/s/a4b39357ea24 HL750 海羐变频器手册详细阐述了通用高性能电流矢量变频器的操作指南,该变频器主要致力于执行和调整三相交流异步电机的运行速度。以下为该手册所包含的核心知识点:变频器基础原理变频器作为一种电力电子装置,能够将交流电源的电压和频率转换为电机运行所需的参数,以此达成对电机速度的有效管理。HL750 海羐变频器是一款具备广泛适用性和高性能的电流矢量变频器,其优势在于卓越的动态响应、显著的过载承载能力,以及增强了用户自定义选项和后台监控系统的功能。矢量控制方法HL750 海羐变频器运用先进的矢量控制方法,能够确保电机在低速情况下产生高扭矩。矢量控制是控制变频器驱动电机的核心技术之一,通过分离处理电机的电流和电压矢量,实现对电机速度的精确控制。产品性能指标HL750 海羐变频器具备以下性能指标:1. 先进的矢量控制技术2. 低速时实现高扭矩输出3. 出色的动态响应特性4. 强大的过载承载能力5. 用户可编程的灵活性6. 集成后台监控系统7. 支持多种通讯总线8. 兼容多种PG卡等实际应用场景HL750 海羐变频器适用于多种自动化生产设备的动力驱动,包括但不限于:1. 纺织行业2. 造纸工业3. 拉丝工艺4. 机床制造5. 包装机械6. 食品加工7. 风机系统8. 水泵系统参数配置说明HL750 海羐变频器的手册中包含了详尽的参数配置方法,涵盖基本功能设置、电机特性参数、矢量控制参数、V/F 控制参数、输入接口布局、输出接口布局、启动与停止控制、键盘与显示界面、辅助功能选项、故障诊断与保护机制等。部件说明文档中还提供了全面的部件说明,涉及从F0 组到F9 组的所有内容,覆盖了变频器的各个...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值