Tomcat中两个不同项目共享Session
2016-06-08 09:22
288 查看
本文研究的是同一个Tomcat目录下的两个不同的应用共享同一个session。由于每个WEB应用程序都有一个唯一的一个ServletContext实例对象,本应用中的所有的servlet共享此ServletContext。利用ServletContext中的setAttribute()方法把Session传递过去
然后在另外一个WEB程序中拿到session实例。
一、修改Tomcat中conf的server.xml文件
修改为:
crossContext属性在帮助文档中意思:
crossContext: Set to true if you want calls within this application
to ServletContext.getContext() to successfully return a request
dispatcher for other web applications running on this virtual host.
Set to false (the default) in security conscious environments, to
make getContext() always return null.
设置为true说明可以调用另外一个WEB应用程序,通过ServletContext.getContext()
获得ServletContext然后再调用其getattribute()得到对象。
二、在项目A中,写入以下代码:
项目A为/projectA
项目B为/projectB
项目A中设置Session:
HttpSession session = req.getSession();
session.setAttribute("name", "Tom");
session.setMaxInactiveInterval(6565);
ServletContext ContextA
=req.getSession().getServletContext();
ContextA.setAttribute("session", req.getSession());
项目B中取出Session:
HttpSession session1 =req .getSession();
ServletContext Context = session1.getServletContext();
ServletContext Context1= Context.getContext("/myweb"); //
项目A的虚拟路径
HttpSession session2
=(HttpSession)Context1.getAttribute("session");
System.out.println("base传过来的user为:"+session2.getAttribute("name"));
原帖地址:http://www.codesky.net/article/201104/174499.html
然后在另外一个WEB程序中拿到session实例。
一、修改Tomcat中conf的server.xml文件
修改为:
crossContext属性在帮助文档中意思:
crossContext: Set to true if you want calls within this application
to ServletContext.getContext() to successfully return a request
dispatcher for other web applications running on this virtual host.
Set to false (the default) in security conscious environments, to
make getContext() always return null.
设置为true说明可以调用另外一个WEB应用程序,通过ServletContext.getContext()
获得ServletContext然后再调用其getattribute()得到对象。
二、在项目A中,写入以下代码:
项目A为/projectA
项目B为/projectB
项目A中设置Session:
HttpSession session = req.getSession();
session.setAttribute("name", "Tom");
session.setMaxInactiveInterval(6565);
ServletContext ContextA
=req.getSession().getServletContext();
ContextA.setAttribute("session", req.getSession());
项目B中取出Session:
HttpSession session1 =req .getSession();
ServletContext Context = session1.getServletContext();
ServletContext Context1= Context.getContext("/myweb"); //
项目A的虚拟路径
HttpSession session2
=(HttpSession)Context1.getAttribute("session");
System.out.println("base传过来的user为:"+session2.getAttribute("name"));
原帖地址:http://www.codesky.net/article/201104/174499.html
相关文章推荐
- Tomcat部署时DefaultAnnotationPro…
- 从tomcat下载带有汉子的资源,无法下载的问题。
- Tomcat日志系统详解
- 在tomcat下部署solr
- Tomcat 7最大并发连接数的正确修改方法(转)
- tomcat上servlet程序的配置与处理servlet请求过程
- tomcat 相关文件说明
- Eclipse中的tomcat怎么加载Maven WEB工程
- eclipse启动tomcat无法访问的解决方法
- 使用Tomcat搭建服务器并配置项目
- hjr-Android:下载tomcat服务器文件
- 配置Tomcat服务器数据连接池
- Tomcat如何禁用session(Turn off the Session in Tomcat )
- tomcat热部署实现文件下载
- 关于tomcat作为服务器 URL访问的一些问题
- 如何把tomcat服务加到Windows系统服务
- tomcat那些事
- linux服务器配置tomcat
- Jersey 2.17 + Tomcat + Maven 构建restful service 实例
- IntelliJ IDEA 14.x 与 Tomcat 集成,创建并运行Java Web项目