java - Under what conditions is a JSESSIONID created? - Stack Overflow
JSESSIONID cookie is created/sent when session is created. Session is created when your code calls request.getSession()
or request.getSession(true)
for the first time. If you just want get session, but not create it if it doesn't exists, use request.getSession(false)
-- this will return you a session or null
. In this case, new session is not created, and JSESSIONID cookie is not sent. (This also means that session isn't necessarily created on first request... you and your code is in controlwhen the session is created)
Sessions are per-context:
SRV.7.3 Session Scope
HttpSession objects must be scoped at the application (or servlet context) level. The underlying mechanism, such as the cookie used to establish the session, can be the same for different contexts, but the object referenced, including the attributes in that object, must never be shared between contexts by the container.
Update: Every call to JSP page implicitly creates new session if there is no session yet. This can be turned off by session='false'
page directive, in which case session variable is not available on JSP page at all.
Tomcat - User - How does one control what the path is on the JSESSIONID cookie?
The client may have more than one session, which must be distinguished
by the path, eg i need:
Set-Cookie: JSESSIONID=6D839FF3B960947CC6FD41B98CD02E0D; Path=/thredds/p1
A previous post had this filter, which im guessing i can modify :
package com.prosc.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
/**
* This class will set the cookie maxAge to match the session timeout value. That way, a user who closes their browser and
* re-enters the site will still have the same session if it has not timed out on the server.
*/
public class SessionCookieExtender implements Filter {
private static final String JSESSIONID = "JSESSIONID";
public void init( FilterConfig config ) throws ServletException {}
public void doFilter( ServletRequest _request, ServletResponse _response, FilterChain chain ) throws IOException, ServletException {
if( _response instanceof HttpServletResponse ) {
HttpServletRequest httpRequest = (HttpServletRequest)_request;
HttpServletResponse httpResponse = (HttpServletResponse)_response;
HttpSession session = httpRequest.getSession();
if( session != null && session.getId() != null ) {
Cookie sessionCookie = new Cookie( JSESSIONID, session.getId() );
int sessionTimeoutSeconds = session.getMaxInactiveInterval();
sessionCookie.setMaxAge( sessionTimeoutSeconds );
sessionCookie.setPath( httpRequest.getContextPath() );
httpResponse.addCookie( sessionCookie ); //FIX! This doesn't actually get rid of the other cookie, but it seems to work OK
}
}
chain.doFilter( _request, _response );
}
public void destroy() {}
}
解决在iframe中进行跨域访问时session丢失的问题:WebLogic之Session - 咫尺天涯 - ITeye技术网站
WebLogic的Cookie相关配置:
属性名 默认值 值
cookie-name JSESSIONID 如未设置,默认为“JSESSIONID”
cookie-path NULL 如未设置,默认为“/”
cookie-domain NULL 如未设置,默认为发放cookie的服务器的域
一个cookie的NAME、Domain和Path属性值均相同,则会覆盖,若未设置Domain域,则域为ip(不包括端口),因此三个应用的session cookie就会互相覆盖
解决办法:设置各个应用使用不同的cookie-name,或者将JSESSIONID的path路径设置为不同
由于没有在Weblogic.xml配置文件中对cookie的相关属性值进行配置,因此应用CA和应用CB的cookie的Name、Domain和Path属性值均为默认值,即Name为JSESSIONID,Path为“/”,Domain为服务器的IP地址,三个属性值均相同,这就造成了应用CA的cookie与应用CB的cookie会互相覆盖,从而导致相应应用的session丢失。
参考资料
1 关于WebLogic的Session丢失的问题
http://blog.csdn.net/DesignLife/article/details/2552186
2 tomcat向weblogic移植出现的问题系列 - session丢失问题
(其中一位网友回答不知是什么意思,要加什么文件)
http://netliving.iteye.com/blog/148485
3 java weblogic session 丢失
http://hi.baidu.com/leftstone/blog/item/3b34a4a19d4d0588471064f0.html
4 weblogic两个domain中jsp相互调用session丢失解决方法
http://www.4ucode.com/Study/Topic/1159787
5 关于WebLogic的Session丢失的问题
http://hi.baidu.com/goylsf/blog/item/d2446677d71e070ab051b98d.html
6 weblogic通过代理插件weblogic.servlet.proxy.HttpProxyServlet跨域访问导致session丢失问题的解决
http://ribbonchen.blog.163.com/blog/static/1183165052011074500877/
7 WebLogic如何设置session超时时间
http://tonyaction.blog.51cto.com/227462/201900
注意网上有很多的Webloigc不是最新的配置
位于WEB-INF/WebLogic.xml配置如下:
- <?xml version='1.0' encoding='utf-8'?>
- <weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
- http://www.bea.com/ns/weblogic/90
- http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">
- <container-descriptor>
- <prefer-web-inf-classes>true</prefer-web-inf-classes>
- </container-descriptor>
- <context-root>/</context-root>
- <session-descriptor>
- <!--WebLogic设置Session超时时间,在web.xml中去掉session超时设置 -->
- <!-- <timeout-secs>7200</timeout-secs> -->
- <cookie-name>JSESSIONID1</cookie-name>
- </session-descriptor>
- </weblogic-web-app>
一 WebLogic设置Session超时
1 web.xml
设置WEB应用程序描述符web.xml里的<session-timeout>元素。这个值以分钟为
单位,并覆盖weblogic.xml中的TimeoutSecs属性
- <session-config>
- <session-timeout>30</session-timeout>
- </session-config>
此例表示Session将在54分钟后过期
当<session-timeout>设置为-2,表示将使用在weblogic.xml中设置的
TimeoutSecs这个属性值。
当<session-timeout>设置为-1,表示Session将永不过期,而忽略在
weblogic.xml中设置的TimeoutSecs属性值。
该属性值可以通过console控制台来设置
在weblgoic的console中:xxDomain->Servers->xxServer->Protocols->HTTP 中有一个关于Post Timeout的配置,但这个参数一般使用默认值即可
一般是通过Services-->JDBC-->Connection Pools-->MyConnection(你所建立的连接池名)-->Configration-->Connections 里的Inactive Connection Timeout这个参数来设置的,默认的为0,表示连接时间无限长。你可以设一个时间值,连接超过这个时间值,它会把连接强制放回连接池
- <Server AcceptBacklog="62" CompleteHTTPMessageTimeout="480"
- CompleteMessageTimeout="480" IdleC
- ListenAddress="" ListenPort="7001" Name="myserver"
- NativeIOEnabled="true" ReliableDeliveryPolicy="RMDefaultPolicy"
- ServerVersion="8.1.4.0">
是否IdleConnectionTimeout参数
2 weblogic.xml
设置WebLogic特有部署描述符weblogic.xml的<session-descriptor>元素的
TimeoutSecs属性。这个值以秒为单位
- <session-descriptor>
- <!--WebLogic设置Session超时时间,在web.xml中去掉session超时设置 -->
- <timeout-secs>7200</timeout-secs>
- </session-descriptor>
二 防止Webloigic下的iframe的Session失效
请在Weblogic.xml添加如下代码
- <session-descriptor>
- <cookie-name>JSESSIONID1</cookie-name>
- </session-descriptor>
原来session在服务器端生成后分配的sessionID在客户端的保存方式是个cookie,它的生命周期在浏览器关闭后就会结束,而这个cookie的名字如果不特别设置,weblogic会以默认的名称“JSESSIONID”来设置这个cookie的名称,我两个应用的的session cookie名字都没有设置,客户端在第一次通过应用A请求代理转发到应用B时,应用B返回的同名session cookie覆盖了客户端原本的应用A的session cookie,所以导致了应用A session的丢失。
解决方法是,在应用B的weblogic.xml中的session descriptor标记中添加session cookie的名称设置,使其区别于A应用的session cookie名称.其中的cookie-name可以重命名!解决在iframe中进行跨域访问时session丢失的问题