0

I have 2 projects. Both of them are Spring MVC projects. They are run by Tomcat8 + Eclipse.

I want to share the session between those 2 projects.

Following what Tomcat’s document (Clustering/Session Replication HOW-TO) said, I added <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> into server.xml. It becomes:

<?xml version="1.0" encoding="UTF-8"?>    
<Server port="8005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>       
    <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
    <GlobalNamingResources>
        <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
    </GlobalNamingResources>        
    <Service name="Catalina">
        <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
        <Connector URIEncoding="utf-8" port="8009" protocol="AJP/1.3" redirectPort="8443"/>
        <Engine defaultHost="localhost" name="Catalina">    
            <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
            </Realm>
            <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>
                <Context docBase="App1" path="/app1" reloadable="true" source="org.eclipse.jst.jee.server:App1"/>
                <Context docBase="App2" path="/app2" reloadable="true" source="org.eclipse.jst.jee.server:App2"/>
            </Host>
        </Engine>
    </Service>
</Server>

And I add <distributable/> into WEB-INF/web.xml of both projects. Their web.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <distributable/>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

However, their session do not shared. To check the cookie, their JSESSIONID’s path and value are different.

How do I share the session between those 2 projects?

PS: I tried https://stackoverflow.com/a/19391515/1618596, but still cannot work

Community
  • 1
  • 1
Shiyou
  • 121
  • 1
  • 4
  • 16
  • Where are your servers running? Does your network allow muticast? Further, from [the documentation](https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html): "_The IP broadcasted is `java.net.InetAddress.getLocalHost().getHostAddress()` (make sure you don't broadcast `127.0.0.1`, this is a common error)_". So, what address are your broadcasting? – Boris the Spider Aug 18 '16 at 08:01
  • I run it in my local mechine. I only put `` so I guess it use the default setting. – Shiyou Aug 18 '16 at 08:12
  • I've tried to setting `228.0.0.4`(as the documents’ example) but still not work. – Shiyou Aug 18 '16 at 08:19

0 Answers0