在项目A中,登录时,将session放到context中:
ServletContext ContextA = session.getServletContext();
ContextA.setAttribute("Appsess", session);
HttpSession se = (HttpSession)ContextA.getAttribute("Appsess");
在项目B中,写一个过滤器,过滤器中的代码如下:
……
HttpSession session1 = (HttpSession) arg0.getSession();;
ServletContext Context = session1.getServletContext();
ServletContext ContextA= Context.getContext("/sale_2012");
HttpSession session2 =(HttpSession)ContextA.getAttribute("Appsess"); ……tomcat的server配置如下:
<Host name="127.0.0.1" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/sale_2012" path="" reloadable="true" crossContext="true" >
<Resource name="jdbc/default" auth="Container" description="DB Connection" type="javax.sql.DataSource"
maxActive="20"
maxIdle="4"
maxWait="10000"
username="***"
password="***"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"
/>
</Context>
</Host> <Host name="127.0.0.1" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/springmvcweb" path="" reloadable="true" crossContext="true" >
<Resource name="jdbc/default" auth="Container" description="DB Connection" type="javax.sql.DataSource"
maxActive="20"
maxIdle="4"
maxWait="10000"
username="***"
password="***"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"
/>
</Context>
</Host>但从项目B中的过滤器中由context中取出来的session2就是为空,百思不得其解。请求大神指点!tomcat session共享
ServletContext ContextA = session.getServletContext();
ContextA.setAttribute("Appsess", session);
HttpSession se = (HttpSession)ContextA.getAttribute("Appsess");
在项目B中,写一个过滤器,过滤器中的代码如下:
……
HttpSession session1 = (HttpSession) arg0.getSession();;
ServletContext Context = session1.getServletContext();
ServletContext ContextA= Context.getContext("/sale_2012");
HttpSession session2 =(HttpSession)ContextA.getAttribute("Appsess"); ……tomcat的server配置如下:
<Host name="127.0.0.1" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/sale_2012" path="" reloadable="true" crossContext="true" >
<Resource name="jdbc/default" auth="Container" description="DB Connection" type="javax.sql.DataSource"
maxActive="20"
maxIdle="4"
maxWait="10000"
username="***"
password="***"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"
/>
</Context>
</Host> <Host name="127.0.0.1" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/springmvcweb" path="" reloadable="true" crossContext="true" >
<Resource name="jdbc/default" auth="Container" description="DB Connection" type="javax.sql.DataSource"
maxActive="20"
maxIdle="4"
maxWait="10000"
username="***"
password="***"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"
/>
</Context>
</Host>但从项目B中的过滤器中由context中取出来的session2就是为空,百思不得其解。请求大神指点!tomcat session共享
在C:\WINDOWS\system32\drivers\etc下的hosts文件内,加两映射
www.a.com 127.0.0.1
www.b.com 127.0.0.1用这俩网站访问俩项目。
你先试试,如果不行,我把我的例子描述给你。
<Context crossContext="true" docBase="d:/xxx/tomcat/webapp/sale_2012" path="/sale_2012" reloadable="false"/>