2个不同的web项目,如何实现session共享
同时启动了2个web项目,如何让它们实现session共享啊。在一个项目中登录之后跳到另一个项目,能直接获取登录名。我通过更改tomcat下面的server.xml配置文件,在程序中通过把session存放在ServletContext,然后在另一个项目中来获得session,但是总是在第二个项目中无法获得ServletContext。
server.xml配置如下:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<context path="/szdai" reloadable="true" crossContext="true"></context>
<context path="/bbs" reloadable="true" crossContext="true"></context>
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->
</Host>
java代码如下:
在szdai项目中,request.getSession().setAttribute("member", memberInfo);
ServletContext contextSzd = request.getSession().getServletContext();
contextSzd.setAttribute("appSzd", request.getSession());
注:memberInfo是一个实体对象
在bbs项目中,ServletContext context = request.getSession().getServletContext();
ServletContext context1 = context.getContext("/szdai");
MemberInfoEO memberInfo=null;
if(context1!=null && !"".equals(context1)){
HttpSession sess = (HttpSession) context1.getAttribute("appSzd");
memberInfo = (MemberInfoEO) sess.getAttribute("member");
}