同一Tomcat下不同Web應用之間共享Session會話
實現同一Tomcat下兩個WEB應用之間透過session 共享資料。
檢視tomcat 關於 HTTP Connector 中有個emptySessionPath 其解釋如下:
If set to true, all paths for session cookies will be set to /. This can be useful for portlet specification implementations. If not specified, this attribute is set to false.
A side effect to setting this to true, is that if Tomcat creates a new session it will attempt to use the cookie session id if supplied by the client.
設定為true 發現沒有用,在網上搜了一下方法,基本是這樣的:
由於每個WEB應用程式都有一個唯一的一個ServletContext 例項物件,自己下面的所有的servlet 共享此ServletContext,利用ServletContext 中的setAttribute() 方法把Session 傳遞過去,然後在另外一個WEB程式中拿到session例項。
1、修改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() 得到你要的物件。
2、在 Project_A 中,寫入以下程式碼:
//以下內容用於測試同一tomcat下不同專案之間共享session
HttpSession session = req.getSession();
session.setAttribute("name", "testuser");
session.setMaxInactiveInterval(1800);
ServletContext ContextA =req.getSession().getServletContext();
ContextA.setAttribute("session", req.getSession());
//測試
out.println("IN SessionRangleServlet name : "+session.getAttribute("name"));
3、在 Project_B 中,寫入以下程式碼取出Session
HttpSession session1 = req .getSession();
ServletContext Context = session1.getServletContext();
// 這裡面傳遞的是 Project_A 的虛擬路徑
ServletContext Context1= Context.getContext("/Project_A");
System.out.println(Context1);
HttpSession session2 =(HttpSession)Context1.getAttribute("session");
System.out.println("base傳過來的user為:"+session2.getAttribute("name"));
然後重新部署。
Tomcat下配置Session Cookie位置
最近部署一個Java應用的時候要求Session Cookie位置為根目錄 “/” 而不是 /context。在配置Tomcat的時候碰到了一些問題,把我的解決過程寫下來,希望給碰到同樣問題的朋友一些幫助。
很多時候我們要求 Session Cookie的位置在根目錄“/”這樣多個應用可以互相互動。Tomcat的預設設定Session Cookie是在 /context 下。
在Tomcat 6 下,修改非常簡單,只要在Connector 下增加 emptySessionPath="true" 屬性就能解決了。可是到了Tomcat 7 ,這個配置不起作用了。
於是查了 Servlet 3.0 spec,發現Servlet 3.0 是允許 per-context basis 配置的,那麼沒有理由Tomcat 7 不支援啊。
後來仔細研究了一下 Tomcat 7 的配置,原來Tomcat 7 把這個配置單獨出來了,由一個sessionCookiePath屬性了。
...
最後試驗了一下,一切OK。
檢視tomcat 關於 HTTP Connector 中有個emptySessionPath 其解釋如下:
If set to true, all paths for session cookies will be set to /. This can be useful for portlet specification implementations. If not specified, this attribute is set to false.
A side effect to setting this to true, is that if Tomcat creates a new session it will attempt to use the cookie session id if supplied by the client.
設定為true 發現沒有用,在網上搜了一下方法,基本是這樣的:
由於每個WEB應用程式都有一個唯一的一個ServletContext 例項物件,自己下面的所有的servlet 共享此ServletContext,利用ServletContext 中的setAttribute() 方法把Session 傳遞過去,然後在另外一個WEB程式中拿到session例項。
1、修改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() 得到你要的物件。
2、在 Project_A 中,寫入以下程式碼:
//以下內容用於測試同一tomcat下不同專案之間共享session
HttpSession session = req.getSession();
session.setAttribute("name", "testuser");
session.setMaxInactiveInterval(1800);
ServletContext ContextA =req.getSession().getServletContext();
ContextA.setAttribute("session", req.getSession());
//測試
out.println("IN SessionRangleServlet name : "+session.getAttribute("name"));
3、在 Project_B 中,寫入以下程式碼取出Session
HttpSession session1 = req .getSession();
ServletContext Context = session1.getServletContext();
// 這裡面傳遞的是 Project_A 的虛擬路徑
ServletContext Context1= Context.getContext("/Project_A");
System.out.println(Context1);
HttpSession session2 =(HttpSession)Context1.getAttribute("session");
System.out.println("base傳過來的user為:"+session2.getAttribute("name"));
然後重新部署。
Tomcat下配置Session Cookie位置
最近部署一個Java應用的時候要求Session Cookie位置為根目錄 “/” 而不是 /context。在配置Tomcat的時候碰到了一些問題,把我的解決過程寫下來,希望給碰到同樣問題的朋友一些幫助。
很多時候我們要求 Session Cookie的位置在根目錄“/”這樣多個應用可以互相互動。Tomcat的預設設定Session Cookie是在 /context 下。
在Tomcat 6 下,修改非常簡單,只要在Connector 下增加 emptySessionPath="true" 屬性就能解決了。可是到了Tomcat 7 ,這個配置不起作用了。
於是查了 Servlet 3.0 spec,發現Servlet 3.0 是允許 per-context basis 配置的,那麼沒有理由Tomcat 7 不支援啊。
後來仔細研究了一下 Tomcat 7 的配置,原來Tomcat 7 把這個配置單獨出來了,由一個sessionCookiePath屬性了。
最後試驗了一下,一切OK。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9399028/viewspace-1126368/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Android 7.0應用之間共享檔案Android
- Tomcat通過自帶的Cluster方式實現Session會話共享環境操作記錄TomcatSession會話
- 快速實現 Tomcat 叢集 Session 共享TomcatSession
- Cassandra的Session會話Session會話
- 會話技術之 Session會話Session
- 會話層技術-session會話Session
- 10、flask-會話-sessionFlask會話Session
- Nginx搭建Tomcat9叢集並實現Session共享NginxTomcatSession
- session 共享Session
- 同一會話中的多個 WebRequest會話Web
- tomcat 設定session過期時間(四種方式)TomcatSession
- Tomcat通過Redis實現session共享的完整部署記錄TomcatRedisSession
- 如何在多臺 Web 伺服器上共享 sessionWeb伺服器Session
- iOS應用之間的跳轉解析iOS
- win10怎麼在同一網路下共享檔案_win10同一網路下如何共享檔案Win10
- ZooKeeper如何模擬會話失效(Session Expired)會話Session
- 令牌Token和會話Session原理與攻略會話Session
- MQTT 持久會話與 Clean Session 詳解MQQT會話Session
- Python 在同一/或不同PDF之間複製頁面Python
- 同一專案、不同版本之間原始碼的閱讀原始碼
- 同一個POD中預設共享哪些名稱空間
- 頁面和應用之間的互動
- session共享問題???Session
- 次世代的會話管理專案 Spring Session會話SpringSession
- nodejs學習08——會話控制 session cookie tokenNodeJS會話SessionCookie
- oracle 會話(session)被鎖瞭解決方法Oracle會話Session
- 同一excel檔案中不同sheet間alt+tab切換Excel
- 【轉】Docker部署Tomcat及Web應用DockerTomcatWeb
- Google BERT中文應用之《紅樓夢》對話人物提取Go
- Tomcat中的session實現TomcatSession
- Tomcat 中的 Session 和 CookieTomcatSessionCookie
- nginx同一埠配置代理不同路徑下的檔案Nginx
- Linux終端會話實時共享(kibitz)Linux會話
- [Web][Tomcat]Tomcat相關WebTomcat
- Cookie和Session有什麼不同?學習web網路安全得多久CookieSessionWeb
- 巨杉核心筆記(一)| SequoiaDB 會話(session)簡介筆記會話Session
- python+pytest介面自動化(10)-session會話保持PythonSession會話
- SpringSession+Redis實現叢集會話共享SpringGseSessionRedis會話
- MySQL的共享鎖阻塞會話案例淺析MySql會話