鉴于大家对JAVA十分关注,我们编辑小组在此为大家搜集整理了“实现二级域名session共享的两种方法”一文,供大家参考学习
两种方法各有利弊,第一种方法编码方式改变比较大,开发人员可能不习惯,但是不受服务器类型的影响;第二种方法,针对tomcat服务器,需要修改tomcat源码,但是不需要重构代码,代码中对session的操作还和以前一样。第二种方式主要做两处改动:1,使用memcache来覆盖tomcat对session的实现。将session存入memcache中。package org.apache.catalina.session;import java.io.*;import com.danga.MemCached.*;import org.apache.catalina.*;public class MemcachedManager extends StandardManager { protected MemCachedClient mc = null; protected SockIOPool pool = null; protected String sockPoolName = "snasessionsock"; protected String serverlist = "192.168.2.194:12000"; protected String snaidPerfix = "snaid"; protected String snaidFlag = "true"; public MemcachedManager() { super(); } public Session findSession(String id) throws IOException { Session session = super.findSession(id); if (session == null && id != null) { try { Object sid = mc.get(this.getSnaidPerfix() + id); if (sid != null) { session = createSession(id); } } catch (Exception ex) { ex.printStackTrace(); } } return session; } public Session createSession(String sessionId) { Session session = super.createSession(sessionId); mc.set(this.getSnaidPerfix() + session.getId(), snaidFlag); return session; } protected StandardSession getNewSession() { return new MemcachedSession(this, mc); } protected void initPool() { try { if (pool == null) { try { pool = SockIOPool.getInstance(); pool.setServers(serverlist.split(",")); pool.setInitConn(5); pool.setMinConn(5); pool.setMaxConn(50); pool.setMaint