1
基於下面SO帖子,我試圖在兩個應用程序上下文之間共享會話(在同一個Tomcat實例上)。上下文之間的會話共享不能在Tomcat 7
Sharing session data between contexts in Tomcat
我已經創建了兩個類似的webapps下面對此進行測試。 (每個web應用僅包含一個servlet和一個web.xml)
Web應用程序-1的Servlet
public class App1Servlet extends HttpServlet
{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession(true);
session.setAttribute("message", "hello");
try{
response.getOutputStream().print("session value set");
}catch(Exception e){}
}
}
的webapp-1的web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>app1</display-name>
<servlet>
<servlet-name>app1servlet</servlet-name>
<servlet-class>session.test.App1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app1servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<cookie-config>
<name>APPSESSIONID</name>
<path>/</path>
</cookie-config>
</session-config>
</web-app>
Web應用程序-2的Servlet
public class App2Servlet extends HttpServlet
{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession(false);
try{
if(session != null){
response.getOutputStream().print(""+session.getAttribute("message"));
} else {
response.getOutputStream().print("session is null");
}
}catch(Exception e){}
}
}
Webapp-2 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>app2</display-name>
<servlet>
<servlet-name>app2servlet</servlet-name>
<servlet-class>session.test.App2Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app2servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<cookie-config>
<name>APPSESSIONID</name>
<path>/</path>
</cookie-config>
</session-config>
</web-app>
現在如果我火了以下http陸續請求一個,第二個請求需要打印「你好」,但是第二次請求總是打印「會話爲空」
http://localhost/app1
http://localhost/app2
可有人請指出這裏有什麼問題? (我的web.xml版本爲3.0)
我正在開發一種社交網絡類的webapp。我打算將UI部分創建爲一個Web應用程序,將後端創建爲一個寧靜的服務Web應用程序,並計劃將這兩個Web應用程序部署到同一個Tomcat實例。任何人都可以建議這是一個正確的方法嗎?