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中的数据。
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中的数据。
相关文章推荐
- 同一个Tomcat不同Web应用之间共享会话Session
- Linux+Nginx+Tomcat+Redis实现负载均衡,应用集群及session共享
- session共享问题(一)----同一tomcat多个应用session问题
- 在同一个tomcat下,不同的web项目共享session
- nginx+tomcat反向代理下使用tomcat-redis-session-manager进行session共享中值得注意的一个问题
- 在同一个tomcat下,不同的web项目共享session
- Asp.net中处理一个站点不同Web应用共享Session的问题
- Tomcat多应用共享Session
- 同一个tomcat多个项目共享session,一个tomcat两个项目共享sessionId
- 同一个tomcat多个web应用共享session
- 在同一个tomcat下,不同的web项目共享session
- 在同一个tomcat下,不同的web项目共享session
- Asp.net中处理一个站点不同Web应用共享Session的问题
- Tomcat多应用共享Session
- 同一tomcat下实现多应用session共享 单点登录
- tomcat server.xml配置crossContext 实现多应用中session共享
- Tomcat多应用共享Session
- 在同一个tomcat下,不同的web项目共享session
- memcached实现多个tomcat共享一个session
- Tomcat 多应用共享session