Tomcat多应用共享Session
2017-03-06 15:30
363 查看
1. 新建两个应用:
test1
|------WEB-INF
| |------web.xml
|------test.jsp
test.jsp代码
[java]
view plain
copy
print?
<%
session.setAttribute("test1Session","test1Session");
session.getServletContext().setAttribute("t1_session",session);
out.println("Application /test1 is ok!<br>");
if(session.getServletContext().getContext("/test2")!=null){
HttpSession t2_session = (HttpSession)session.getServletContext().getContext("/test2").getAttribute("t2_session");
if(t2_session!=null){
String str = (String)t2_session.getAttribute("test2Session");
String path = request.getContextPath();
out.println("Application "+path +":"+str+"<br>");
}else{
out.println("Application /test2 no data!");
}
}
%>
test2
|------WEB-INF
| |------web.xml
|------test.jsp
test.jsp代码
[java]
view plain
copy
print?
<%
session.setAttribute("test2Session","test2Session");
session.getServletContext().setAttribute("t2_session",session);
out.println("Application /test2 is ok!<br>");
if(session.getServletContext().getContext("/test1")!=null){
HttpSession t1_session = (HttpSession)session.getServletContext().getContext("/test1").getAttribute("t1_session");
if(t1_session!=null){
String str = (String)t1_session.getAttribute("test1Session");
String path = request.getContextPath();
out.println("Application "+path +":"+str+"<br>");
}else{
out.println("Application /test1 no data!");
}
}
%>
2. 配置tomcat,设置crossContext = true,让两个应用可以在tomcat中交叉使用上下文环境。
<Context path="/test1" docBase="C:/test1" reloadable="true" crossContext="true"></Context>
<Context path="/test2" docBase="C:/test2" reloadable="true" crossContext="true"></Context>
3. 启动Tomcat,访问http://localhost:8080/test1/test.jsp
页面输出:Application /test1 is ok!------------------------test1存储session成功
Application /test2 no data!--------------------test2还没有被访问,所以test2的session中没有数据
访问http://localhost:8080/test2/test.jsp
页面输出:Application /test2 is ok!------------------------test2存储session成功
Application /test2:test1Session-------------test1Session来自test1应用中session
刷新http://localhost:8080/test1/test.jsp
页面输出:Application /test1 is ok!------------------------test1存储session成功
Application /test1:test2Session-------------test2Session来自test2应用中session
应用test1和test2成功共享session,可以互相访问另一个应用中的session和session中的数据。
test1
|------WEB-INF
| |------web.xml
|------test.jsp
test.jsp代码
[java]
view plain
copy
print?
<%
session.setAttribute("test1Session","test1Session");
session.getServletContext().setAttribute("t1_session",session);
out.println("Application /test1 is ok!<br>");
if(session.getServletContext().getContext("/test2")!=null){
HttpSession t2_session = (HttpSession)session.getServletContext().getContext("/test2").getAttribute("t2_session");
if(t2_session!=null){
String str = (String)t2_session.getAttribute("test2Session");
String path = request.getContextPath();
out.println("Application "+path +":"+str+"<br>");
}else{
out.println("Application /test2 no data!");
}
}
%>
<% session.setAttribute("test1Session","test1Session"); session.getServletContext().setAttribute("t1_session",session); out.println("Application /test1 is ok!<br>"); if(session.getServletContext().getContext("/test2")!=null){ HttpSession t2_session = (HttpSession)session.getServletContext().getContext("/test2").getAttribute("t2_session"); if(t2_session!=null){ String str = (String)t2_session.getAttribute("test2Session"); String path = request.getContextPath(); out.println("Application "+path +":"+str+"<br>"); }else{ out.println("Application /test2 no data!"); } } %>
test2
|------WEB-INF
| |------web.xml
|------test.jsp
test.jsp代码
[java]
view plain
copy
print?
<%
session.setAttribute("test2Session","test2Session");
session.getServletContext().setAttribute("t2_session",session);
out.println("Application /test2 is ok!<br>");
if(session.getServletContext().getContext("/test1")!=null){
HttpSession t1_session = (HttpSession)session.getServletContext().getContext("/test1").getAttribute("t1_session");
if(t1_session!=null){
String str = (String)t1_session.getAttribute("test1Session");
String path = request.getContextPath();
out.println("Application "+path +":"+str+"<br>");
}else{
out.println("Application /test1 no data!");
}
}
%>
<% session.setAttribute("test2Session","test2Session"); session.getServletContext().setAttribute("t2_session",session); out.println("Application /test2 is ok!<br>"); if(session.getServletContext().getContext("/test1")!=null){ HttpSession t1_session = (HttpSession)session.getServletContext().getContext("/test1").getAttribute("t1_session"); if(t1_session!=null){ String str = (String)t1_session.getAttribute("test1Session"); String path = request.getContextPath(); out.println("Application "+path +":"+str+"<br>"); }else{ out.println("Application /test1 no data!"); } } %>
2. 配置tomcat,设置crossContext = true,让两个应用可以在tomcat中交叉使用上下文环境。
<Context path="/test1" docBase="C:/test1" reloadable="true" crossContext="true"></Context>
<Context path="/test2" docBase="C:/test2" reloadable="true" crossContext="true"></Context>
3. 启动Tomcat,访问http://localhost:8080/test1/test.jsp
页面输出:Application /test1 is ok!------------------------test1存储session成功
Application /test2 no data!--------------------test2还没有被访问,所以test2的session中没有数据
访问http://localhost:8080/test2/test.jsp
页面输出:Application /test2 is ok!------------------------test2存储session成功
Application /test2:test1Session-------------test1Session来自test1应用中session
刷新http://localhost:8080/test1/test.jsp
页面输出:Application /test1 is ok!------------------------test1存储session成功
Application /test1:test2Session-------------test2Session来自test2应用中session
应用test1和test2成功共享session,可以互相访问另一个应用中的session和session中的数据。
相关文章推荐
- 同一tomcat下实现多应用session共享 单点登录
- Tomcat多应用共享Session
- 同一个Tomcat不同Web应用之间共享会话Session
- Nginx+tomcat集群redis共享session应用方案
- Linux+Nginx+Tomcat+Redis实现负载均衡,应用集群及session共享
- 5、SOA模块可插拔设计_Tomcat多应用共享Session
- Tomcat配置多应用共享Session(使用crossContext)
- Tomcat多应用共享Session 使用crossContext
- Nginx+Tomcat+Redis实现应用服务器集群负载均衡和Session共享
- Tomcat多应用共享同一个Session
- Tomcat多应用共享Session
- Tomcat 多应用共享session
- Tomcat多应用共享Session
- tomcat server.xml配置crossContext 实现多应用中session共享
- Windows环境下Nginx+Tomcat+Redis实现应用服务器集群负载均衡和Session共享
- Tomcat多应用共享Session
- session共享问题(一)----同一tomcat多个应用session问题
- JAVA基础应用——tomcat-redis-session-manager Redis共享Session
- 跨应用Session共享
- ngnix+ tomcat +memcached 集群设置服务器负载均衡,session共享