您的位置:首页 > 运维架构 > Tomcat

Tomcat多应用共享同一个Session

2013-05-14 00:00 567 查看
1. 新建两个应用:

shareSessionTest1


|------WEB-INF

| |------web.xml

|------test.jsp

test.jsp代码

<%
HttpSession t_session = (HttpSession)session.getServletContext().getAttribute("t_session");
if(t_session != null){
out.println("shareSessionTest1 session:"+t_session.getAttribute("testSession")+"<br>");
}else{
session.setAttribute("testSession","testSession");
session.getServletContext().setAttribute("t_session",session);
out.println("shareSessionTest1 save session:"+session.getAttribute("testSession")+"<br>");
}

out.println("Application /test1 is ok!<br>");

if(session.getServletContext().getContext("/shareSessionTest2")!=null){

HttpSession t2_session = (HttpSession)session.getServletContext().getContext("/shareSessionTest2").getAttribute("t_session");

t2_session = session;

t2_session.setAttribute("testSession","testSession");

session.getServletContext().getContext("/shareSessionTest2").setAttribute("t_session",t2_session);
}
%>

shareSessionTest2

|------WEB-INF

| |------web.xml

|------test.jsp

test.jsp代码

<%
HttpSession t_session = (HttpSession)session.getServletContext().getAttribute("t_session");
if(t_session != null)
{ out.println("shareSessionTest2 session:"+t_session.getAttribute("testSession")+"<br>");
}else{
session.setAttribute("testSession","testSession");
session.getServletContext().setAttribute("t_session",session);
out.println("shareSessionTest2 save session:"+session.getAttribute("testSession")+"<br>");
}

out.println("Application /test2 is ok!<br>");

if(session.getServletContext().getContext("/shareSessionTest1")!=null){
&nb
8000
sp;
HttpSession t1_session = (HttpSession)session.getServletContext().getContext("/shareSessionTest1").getAttribute("t_session");

t1_session = session;

t1_session.setAttribute("testSession","testSession");

session.getServletContext().getContext("/shareSessionTest2").setAttribute("t_session",t1_session);
}
%>

2. 配置tomcat,设置crossContext = true,让两个应用可以在tomcat中交叉使用上下文环境。

<Context docBase="shareSessionTest1" path="/shareSessionTest1" reloadable="true" crossContext="true" source="org.eclipse.jst.jee.server:shareSessionTest1"/>
<Context docBase="shareSessionTest2" path="/shareSessionTest2" reloadable="true" crossContext="true" source="org.eclipse.jst.jee.server:shareSessionTest2"/>

3. 启动Tomcat,访问http://localhost:8080/shareSessionTest1/test.jsp

页面输出:shareSessionTest1 save session:testSession
Application /test1 is ok!

作用:向多个应用的session保存同一个session,达到共享session目的。

访问http://localhost:8080/shareSessionTest2/test.jsp

页面输出:shareSessionTest2 session:testSession
Application /test2 is ok!

作用:验证session是否共享成功。

刷新http://localhost:8080/shareSessionTest1/test.jsp

页面输出:shareSessionTest1 session:testSession
Application /test1 is ok!

作用:也是验证session是否共享成功。

注意:shareSessionTest1的test.jsp页面被访问2次,shareSessionTest2的test.jsp页面被访问1次,

一个访问了3次,但是我们的session仅此保存一次。

应用test1和test2成功共享session,可以互相访问另一个应用中的session和session中的数据。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息