声明:是一个tomcat下的不同web项目,不是不同tomcat不同项目的,所有这里不会说用什么集群的方法....
???? 其实也很简单,就是将session保存在ServletContext里面就可以了。
?
???? 在项目A中:
?
???session.setAttribute("user",user);
???ServletContext ContextA =session.getServletContext();
???ContextA.setAttribute("session", session);
?
???? 在项目B中:添加一个过滤器(web.xml配置就不写了)
public class SessionFilter implements Filter { private static final long serialVersionUID = 1L; @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; HttpSession session1 =request.getSession(); ServletContext Context = session1.getServletContext(); ServletContext Context1= Context.getContext("/项目A名称"); if(Context1 !=null && !Context1.equals("")){ HttpSession session =(HttpSession)Context1.getAttribute("session"); String user = null; try{ user =(String) session.getAttribute("user"); }catch (Exception e) { } request.setAttribute("user", user); } chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("sessionFilter"); } }?
这样在项目A登陆用户 保存session,在项目B就可以获取A的session信息了。当然这只是单向的,双向的方法也一样了
?
注:?? <Context path="/xxx" docBase="xxx" debug="0" reloadable="true" crossContext="true"/>
crossContext必须设置为true,否则没用!!!!
?
?
此文为转载