在java web应用中,session是怎么产生的,sessionId仅仅是一个字符串吗,sessionId的长度是否是固定的还是可以设置的?
这样的几个sessionId是不经过任何加工产生的sessionId吗?
sessionId 1: L9gFLLBYSL0tJG3LCJCLDFGl2Clvpr92fn7cdPxM8GPJvGGhWpG7!-1432758472!1267417592683
sessionId 2: HnyZLLCLQL2snK2gSLJ2Whn2XvxLsT9G8dxCfgQtLN16WNhTbfpB!-1432758472!1267417611340
sessionId 3: QtLLLLCYjwwZSKQQJ7x2XpgdsLWGmCnhpTPwjG2G0CYKsQHSVhXF!-1432758472!1267417688388
这样的几个sessionId是不经过任何加工产生的sessionId吗?
sessionId 1: L9gFLLBYSL0tJG3LCJCLDFGl2Clvpr92fn7cdPxM8GPJvGGhWpG7!-1432758472!1267417592683
sessionId 2: HnyZLLCLQL2snK2gSLJ2Whn2XvxLsT9G8dxCfgQtLN16WNhTbfpB!-1432758472!1267417611340
sessionId 3: QtLLLLCYjwwZSKQQJ7x2XpgdsLWGmCnhpTPwjG2G0CYKsQHSVhXF!-1432758472!1267417688388
out.println(session.getId());
这样显示出来的是32位的英文数字组合,这样的是原生的吧;
那么我一楼给出的哪些sessionId就是经过加工过了的,对吧。
QtLLLLCYjwwZSKQQJ7x2XpgdsLWGmCnhpTPwjG2G0CYKsQHSVhXF!-1432758472!1267417688388不知道后面由叹号分开的数字是不是代表什么意思呢?
原来是这样
没有规定,JSESSIONID一定保存在cookie里面。并不是每个用户都打开cookie的,开发应用程序不能强迫用户必须打开cookie。更何况很多手机是没有cookie或者缺省是关闭cookie的。
没有cookie的实况下,一般的处理是把jessionid嵌在url里面,比方说
https://localhost/webapp/path/xxxxx.do;jsessionid=351E0B1A660AE167E55D04B9D42A532C?param1=1¶m2=2
jsessionid值的生成还是由server来做,server也要求浏览器生成cookie以保存jsessionid,但是如果浏览器没有cookie或者禁止了cookie,以后发回server的url里面有型如上面的样子,jsessionid一样可以返回到server,以保持会话。
这里要注意的就是,以后不管是FreeMarker的ftl,还是JSP里面,所有的href,action等链接里面jsessionid都要嵌入到目标URL里面,有点麻烦。然后说点小技巧,如何在服务器端判断客户端的浏览器是否允许cookie?或者说sessionID是从cookie中传来的?还是嵌入在URL中传过来的?
可以通过,HttpServletRequest的isRequestedSessionIdFromCookie方法来获得。HttpServletResponse各个web application server的处理细节不同,所以结果也差强人意。这里
只看看tomcat的实装:
程序在isRequestedSessionIdFromCookie方法org.apache.catalina.connector.Request里:
/**
* Return <code>true</code> if the session identifier included in this
* request came from a cookie.
*/
public boolean isRequestedSessionIdFromCookie() { if (requestedSessionId != null)
return (requestedSessionCookie);
else
return (false); }而上面的requestedSessionCookie的设定是在org.apache.catalina.connector的parseSessionCookiesId函数里: /**
* Parse session id in URL.
*/
protected void parseSessionCookiesId(org.apache.coyote.Request req, Request request) { // If session tracking via cookies has been disabled for the current
// context, don't go looking for a session ID in a cookie as a cookie
// from a parent context with a session ID may be present which would
// overwrite the valid session ID encoded in the URL
Context context = (Context) request.getMappingData().context;
if (context != null && !context.getCookies())
return; // Parse session id from cookies
Cookies serverCookies = req.getCookies();
int count = serverCookies.getCookieCount();
if (count <= 0)
return; for (int i = 0; i < count; i++) {
ServerCookie scookie = serverCookies.getCookie(i);
if (scookie.getName().equals(Globals.SESSION_COOKIE_NAME)) {
// Override anything requested in the URL
if (!request.isRequestedSessionIdFromCookie()) {
// Accept only the first session id cookie
convertMB(scookie.getValue());
request.setRequestedSessionId
(scookie.getValue().toString());
request.setRequestedSessionCookie(true);
request.setRequestedSessionURL(false);
if (log.isDebugEnabled())
log.debug(" Requested cookie session id is " +
request.getRequestedSessionId());
} else {
if (!request.isRequestedSessionIdValid()) {
// Replace the session id until one is valid
convertMB(scookie.getValue());
request.setRequestedSessionId
(scookie.getValue().toString());
}
}
}
}
}实际运用中,未必一定要用上面isRequestedSessionIdFromCookie,也可以考虑用response的encodeURL方法,直接获得URL,当然它会在内部调用isRequestedSessionIdFromCookie,以判断是否需要把JesssionID嵌入到URL中。最后,JessionID这个键名,在weblogic里面是可以可以改的(好像是在weblogic.xml里面定义),tomcat比较弱弱,不能改动,因为在org.apache.catalina.Globals里面: /**
* The name of the cookie used to pass the session identifier back
* and forth with the client.
*/
public static final String SESSION_COOKIE_NAME = "JSESSIONID";
/**
* The name of the path parameter used to pass the session identifier
* back and forth with the client.
*/
public static final String SESSION_PARAMETER_NAME = "jsessionid";final掉了。
业务需要更改jessionid的键名的虽然不多,但是也还是有的,这个时候用tomcat就不太合适了。