同一台机器挂载多个tomcat时 session的问题

journeydj 2013-12-27 09:08:13
因为有需要每一个项目有独立端口,并且能够单独启动和关闭,所以在一台服务器上配置了多个tomcat。
tomcat是完全一样的,只是各自的端口不一致。
现在的问题是单独启动一个tomcat完全没有问题。
但是启动两个tomcat的时候,有如下问题
1,第一个项目在登录页面登录成功后进入主页面。
2,第二个项目在登录页面登录成功后进入主页面。
3,刷新第一个项目的主页面被踢出到登陆页,session丢失。
4,刷新第二个项目的主页面也被踢出到登陆页,session同样丢失。

请问tomcat应该如何配置session,才能不发生这样的情况?
...全文
1001 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
windvx 2016-03-24
  • 打赏
  • 举报
回复
项目名称相同的问题, http://localhost:8080/session http://localhost:8181/session 这样分别请求session就会出现相互变化。 http://localhost:8080/session8 http://localhost:8181/session9 server.xml 后面分别配置项目名 <Context path="/session9" docBase="D:\program files\Apache Software Foundation\apache-tomcat-9.0.0.M4\webapps\SessionTest" debug="0" reloadable="true" />
贱起苍澜 2015-12-23
  • 打赏
  • 举报
回复
引用 22 楼 fengcai19xq 的回复:
修改tomcat server.xml 中Context 属性, 添加sessionCookiePath和sessionCookieName配置 例如: sessionCookiePath="/" sessionCookieName="JSESSIONID_cookie1" 或者 sessionCookiePath="/projectName" sessionCookieName="JSESSIONID_cookie2"
我觉着也是这个意思 session就是叫JSESSION的cookie, 两个网站是同一个域名,建立session的时候就会覆盖cookie,感觉
易修& 2015-10-27
  • 打赏
  • 举报
回复
修改tomcat server.xml 中Context 属性, 添加sessionCookiePath和sessionCookieName配置 例如: sessionCookiePath="/" sessionCookieName="JSESSIONID_cookie1" 或者 sessionCookiePath="/projectName" sessionCookieName="JSESSIONID_cookie2"
Strollacc 2014-08-06
  • 打赏
  • 举报
回复
楼主最后如何解决的,我也遇到了同样的问题,谢谢啊!
lk1942 2014-03-26
  • 打赏
  • 举报
回复
在server.xml里Context 标签中加sessionCookieName,两个tomcat取名不一样就可以了,例如一个叫sessionid1一个叫sessionid2 <Context path="" docBase="D:/xxxxx" sessionCookieName="sessionid1"/>
journeydj 2013-12-30
  • 打赏
  • 举报
回复
引用 18 楼 huxiweng 的回复:
你到底是要session共享还是要保持各自的session?
想保持各自的session。
teemai 2013-12-28
  • 打赏
  • 举报
回复
你到底是要session共享还是要保持各自的session?
宁波朱超 2013-12-28
  • 打赏
  • 举报
回复
引用 楼主 journeydj 的回复:
因为有需要每一个项目有独立端口,并且能够单独启动和关闭,所以在一台服务器上配置了多个tomcat。 tomcat是完全一样的,只是各自的端口不一致。 现在的问题是单独启动一个tomcat完全没有问题。 但是启动两个tomcat的时候,有如下问题 1,第一个项目在登录页面登录成功后进入主页面。 2,第二个项目在登录页面登录成功后进入主页面。 3,刷新第一个项目的主页面被踢出到登陆页,session丢失。 4,刷新第二个项目的主页面也被踢出到登陆页,session同样丢失。 请问tomcat应该如何配置session,才能不发生这样的情况?
多个项目放一个TOMCAT可以做到SESSION共享, 多个TOMCAT的话就是多个应用服务器了, 建议楼主用SSO来做。
z_yes163 2013-12-27
  • 打赏
  • 举报
回复
<Connector port="9493" protocol="AJP/1.3" redirectPort="9492" /> 你这个使用了,负载均衡 自己上网找以下负载均衡 tomcat session共享。很简单的
致知Fighting 2013-12-27
  • 打赏
  • 举报
回复
引用 13 楼 u012463264 的回复:
[quote=引用 11 楼 journeydj 的回复:] [quote=引用 10 楼 u012463264 的回复:] 那就是浏览器问题 像7楼说的那样
用两个IE不好用。。。[/quote] sessionID 搞混乱了 , 这sessionID的生成机制,可能和服务器地址,客户端地址有关什么的 ,而你这都是一样的 ,除了端口号[/quote] 我觉得有可能是他的两个项目部署在同一个域名下,浏览器把sessionid搞混了
别闹腰不好 2013-12-27
  • 打赏
  • 举报
回复
引用 11 楼 journeydj 的回复:
[quote=引用 10 楼 u012463264 的回复:] 那就是浏览器问题 像7楼说的那样
用两个IE不好用。。。[/quote] sessionID 搞混乱了 , 这sessionID的生成机制,可能和服务器地址,客户端地址有关什么的 ,而你这都是一样的 ,除了端口号
致知Fighting 2013-12-27
  • 打赏
  • 举报
回复
你两个项目部署的域名是怎么样的?
journeydj 2013-12-27
  • 打赏
  • 举报
回复
引用 10 楼 u012463264 的回复:
那就是浏览器问题 像7楼说的那样
用两个IE不好用。。。
别闹腰不好 2013-12-27
  • 打赏
  • 举报
回复
那就是浏览器问题 像7楼说的那样
journeydj 2013-12-27
  • 打赏
  • 举报
回复
引用 8 楼 u012463264 的回复:
觉得session验证的时候有问题 ,用两个不同的账户 登录试试
就是两个用户 两个网站是完全不同的
别闹腰不好 2013-12-27
  • 打赏
  • 举报
回复
觉得session验证的时候有问题 ,用两个不同的账户 登录试试
Defonds 2013-12-27
  • 打赏
  • 举报
回复
据我所知,chrome 开起来只能是一个 session 吧?
journeydj 2013-12-27
  • 打赏
  • 举报
回复
项目2: <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="9491" shutdown="SHUTDOWN"> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html --> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8099 --> <Connector port="8099" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="9492" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8099" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="9492" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 9492 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="9492" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="9493" protocol="AJP/1.3" redirectPort="9492" /> <!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="localhost"> <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!-- The request dumper valve dumps useful debugging information about the request and response data received and sent by Tomcat. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.RequestDumperValve"/> --> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> <!-- Define the default virtual host Note: XML Schema validation will not work with Xerces 2.2. --> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> --> <Context path="" reloadable="true" docBase="D:\svn\named\app\live_manage" workDir="D:\svn\named\app\live_manage\work" /> </Host> </Engine> </Service> </Server>
journeydj 2013-12-27
  • 打赏
  • 举报
回复
引用 3 楼 defonds 的回复:
方便的话,把两个 tomcat 的 server.xml 贴一下,大家看看
项目1: <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="9691" shutdown="SHUTDOWN"> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html --> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8098 --> <Connector port="8098" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="9692" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8098" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="9692" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 9692 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="9692" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="9693" protocol="AJP/1.3" redirectPort="9692" /> <!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="localhost"> <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!-- The request dumper valve dumps useful debugging information about the request and response data received and sent by Tomcat. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.RequestDumperValve"/> --> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> <!-- Define the default virtual host Note: XML Schema validation will not work with Xerces 2.2. --> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> --> <Context path="" reloadable="true" docBase="D:\svn\named\app\live" workDir="D:\svn\named\app\live\work" /> </Host> </Engine> </Service> </Server>
journeydj 2013-12-27
  • 打赏
  • 举报
回复
引用 2 楼 u012463264 的回复:
项目问题吧 你用两台机器分别登录两个项目也有这问题吗
两台机器没问题 同一台机器 同一种浏览器才有这个问题 一个IE 一个chrome没问题。
加载更多回复(4)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧