问题描述
基于下面的 SO 帖子,我试图在两个应用程序上下文(在同一个 Tomcat 实例上)之间共享会话.
Based on the below SO post, I am trying to share session between two application contexts (on the same Tomcat instance).
我创建了两个像下面这样的 web 应用程序来测试这个.(每个 webapp 只包含一个 servlet 和一个 web.xml)
I have created two webapps like the following to test this. (Each webapp contains only a servlet and a web.xml)
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>
WebApp-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请求,第二个请求需要打印hello",但第二个请求总是打印session is null"
Now if I fire the following http requests one after another, the second request need to print "hello", But the second request is always printing "session is null"
http://localhost/app1
http://localhost/app2
有人可以指出这里有什么问题吗?(我的 web.xml 是 3.0 版)
Can anybody please point out what is wrong here? (My web.xml is at version 3.0)
我正在开发一种社交网络类型的网络应用程序.我计划将 UI 部分创建为一个 webapp,并将后端创建为一个安静的服务 webapp,并计划将两个 webapp 部署到同一个 tomcat 实例.有人可以建议这是一种正确的方法吗?
I am developing a social-networking kind of webapp. I am planning to create the UI part as one webapp and back-end as a restful service webapp and planning to deploy both webapps to the same tomcat instance. Can anybody suggest this is a right approach?
推荐答案
会话永远不会在 Web 应用程序之间共享,尽管会话 ID 可能取决于配置.当一个 ID 被共享时,会话会以正常方式在每个 Web 应用程序中创建,但它们将共享一个公共 ID.
Sessions are never shared between web applications although session IDs might be depending on configuration. When an ID is shared sessions are created in each web application in the normal manner but they will share a common ID.
这篇关于上下文之间的会话共享在 Tomcat 7 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!