---
title: "Session对象的创建"
author: "113.com"
date: "2013-04-16T09:49:20Z"
tags: []
categories: ["Web前端"]
slug: "session"
draft: false
lastmod: "2013-04-13T15:31:26Z"
id: 1846822
url: "https://113-com.iteye.com/blog/1846822"
---

Session对象的创建一般是源于这样的一条语句：   
Session session = request.getSession(false);或者Session session = request.getSession();如果不在乎服务器压力可能多那么一点点的话。 

在Tomcat的实现中，这个request是org.apache.catalina.connector.Request类的包装类org.apache.catalina.connector.RequestFacade的对象，它的两个#getSession()方法如下： 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

01. public HttpSession getSession(boolean create) {
02. if (request == null) {
03. throw new IllegalStateException(
04. sm.getString("requestFacade.nullRequest"));
05. }
06. if (SecurityUtil.isPackageProtectionEnabled()){
07. return (HttpSession)AccessController.
08. doPrivileged(new GetSessionPrivilegedAction(create));
09. } else {
10. return request.getSession(create);
11. }
12. }

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

1. public HttpSession getSession() {
2. if (request == null) {
3. throw new IllegalStateException(
4. sm.getString("requestFacade.nullRequest"));
5. }
6. return getSession(true);
7. }

其实差不太多，最后都会进入org.apache.catalina.connector.Request的#getSession()方法。这个方法的源代码如下： 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

1. public HttpSession getSession(boolean create) {
2. Session session = doGetSession(create);
3. if (session != null) {
4. return session.getSession();
5. } else {
6. return null;
7. }
8. }

然后调用就到了#doGetSession()这个方法了。源代码如下 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

01. protected Session doGetSession(boolean create) {
02. // 没有Context的话直接返回null
03. if (context == null)
04. return (null);
05. // 判断Session是否有效
06. if ((session != null) && !session.isValid())
07. session = null;
08. if (session != null)
09. return (session);
10. // 返回Manager对象，这里是StandardManager类的对象
11. Manager manager = null;
12. if (context != null)
13. manager = context.getManager();
14. if (manager == null)
15. return (null); // Sessions are not supported
16. // 判断是否有SessionID
17. if (requestedSessionId != null) {
18. try {
19. // 在Manager中根据SessionID查找Session
20. session = manager.findSession(requestedSessionId);
21. } catch (IOException e) {
22. session = null;
23. }
24. if ((session != null) && !session.isValid())
25. session = null;
26. if (session != null) {
27. // 更新访问时间
28. session.access();
29. return (session);
30. }
31. }
32. // 创建新的Session
33. if (!create)
34. return (null);
35. if ((context != null) && (response != null) && context.getCookies()
36. && response.getResponse().isCommitted()) {
37. throw new IllegalStateException(sm.getString("coyoteRequest.sessionCreateCommitted"));
38. }
39. // 判断是否使用 "/" 作为Session Cookie的存储路径 并且 是否SessionID来自Cookie
40. if (connector.getEmptySessionPath() && isRequestedSessionIdFromCookie()) {
41. // 创建Session
42. session = manager.createSession(getRequestedSessionId());
43. } else {
44. session = manager.createSession(null);
45. }
46. // 创建一个新的Session Cookies
47. if ((session != null) && (getContext() != null) && getContext().getCookies()) {
48. Cookie cookie = new Cookie(Globals.SESSION\_COOKIE\_NAME, session.getIdInternal());
49. // 配置Session Cookie
50. configureSessionCookie(cookie);
51. // 在响应中加入Session Cookie
52. response.addCookieInternal(cookie);
53. }
54. if (session != null) {
55. // 更新访问时间
56. session.access();
57. return (session);
58. } else {
59. return (null);
60. }
61. }

这个方法说明了Session创建的大致过程，首先判断requestedSessionId是否存在，如果存在，那么根据这个ID去查找Session对象。如果requestedSessionId不存在或者没有取到Session，并且传递给#getSession(boolean)的参数为真，那么要创建一个新的Session，并且给客户端写回去一个Session Cookie。 

首先，我感兴趣的是requestedSessionId的赋值，它到底是什么时候被赋值的呢？ 

还要向回看Tomcat的请求处理过程，请求曾到过这一步，org.apache.catalina.connector.CoyoteAdapter的#service()方法。里边有这样一句方法调用：postParseRequest(req, request, res, response)。就是这一步处理了SessionID的获取，这个方法调用了#parseSessionId()和parseSessionCookiesId()这两个方法，就是它对Session ID进行了提取，源代码分别如下： 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

01. protected void parseSessionId(org.apache.coyote.Request req, Request request) {
02. ByteChunk uriBC = req.requestURI().getByteChunk();
03. // 判断URL中是不是有";jsessionid="这个字符串
04. int semicolon = uriBC.indexOf(match, 0, match.length(), 0);
05. if (semicolon &gt; 0) {
06. // Parse session ID, and extract it from the decoded request URI
07. // 在URL中提取Session ID
08. int start = uriBC.getStart();
09. int end = uriBC.getEnd();
10. int sessionIdStart = semicolon + match.length();
11. int semicolon2 = uriBC.indexOf(';', sessionIdStart);
12. if (semicolon2 &gt;= 0) {
13. request.setRequestedSessionId(new String(uriBC.getBuffer(), start + sessionIdStart,
14. semicolon2 - sessionIdStart));
15. byte\[] buf = uriBC.getBuffer();
16. for (int i = 0; i &lt; end - start - semicolon2; i++) {
17. buf\[start + semicolon + i] = buf\[start + i + semicolon2];
18. }
19. uriBC.setBytes(buf, start, end - start - semicolon2 + semicolon);
20. } else {
21. request.setRequestedSessionId(new String(uriBC.getBuffer(), start + sessionIdStart,
22. (end - start) - sessionIdStart));
23. uriBC.setEnd(start + semicolon);
24. }
25. // 设定Session ID来自于URL
26. request.setRequestedSessionURL(true);
27. } else {
28. request.setRequestedSessionId(null);
29. request.setRequestedSessionURL(false);
30. }
31. }

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

01. protected void parseSessionCookiesId(org.apache.coyote.Request req, Request request) {
02. Context context = (Context) request.getMappingData().context;
03. if (context != null && !context.getCookies())
04. return;
05. // 返回Cookie
06. Cookies serverCookies = req.getCookies();
07. int count = serverCookies.getCookieCount();
08. if (count &lt;= 0)
09. return;
10. for (int i = 0; i &lt; count; i++) {
11. ServerCookie scookie = serverCookies.getCookie(i);
12. // 判断是否有JSESSIONID这个名字的Cookie
13. if (scookie.getName().equals(Globals.SESSION\_COOKIE\_NAME)) {
14. // Override anything requested in the URL
15. if (!request.isRequestedSessionIdFromCookie()) {
16. // 设定Session ID
17. convertMB(scookie.getValue());
18. request.setRequestedSessionId(scookie.getValue().toString());
19. // 如果之前在URL中读到了SessionID，那么会覆盖它
20. request.setRequestedSessionCookie(true);
21. request.setRequestedSessionURL(false);
22. if (log.isDebugEnabled())
23. log.debug(" Requested cookie session id is " + request.getRequestedSessionId());
24. } else {
25. if (!request.isRequestedSessionIdValid()) {
26. convertMB(scookie.getValue());
27. request.setRequestedSessionId(scookie.getValue().toString());
28. }
29. }
30. }
31. }
32. }

Tomcat就是通过上边的两个方法读到URL或者Cookie中存放的Session ID的。 

了解了Session ID的获取，下面要看一下Session的查找过程，就是org.apache.catalina.session.StandardManager的#findSession()方法。这个方法是在它的基类中定义的，源代码如下： 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

1. public Session findSession(String id) throws IOException {
2. if (id == null)
3. return (null);
4. return (Session) sessions.get(id);
5. }

代码很短，其中sessions是一个ConcurrentHashMap&lt;String, Session&gt;对象。那么这个sessions的对象是什么时候载入的Session呢？ 

启动的时候！可以看一下StandardManager#start()方法。最后调用了#load()方法，这个就是载入Session的方法了： 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

01. public void load() throws ClassNotFoundException, IOException {
02. if (SecurityUtil.isPackageProtectionEnabled()) {
03. try {
04. AccessController.doPrivileged(new PrivilegedDoLoad());
05. } catch (PrivilegedActionException ex) {
06. Exception exception = ex.getException();
07. if (exception instanceof ClassNotFoundException) {
08. throw (ClassNotFoundException) exception;
09. } else if (exception instanceof IOException) {
10. throw (IOException) exception;
11. }
12. if (log.isDebugEnabled())
13. log.debug("Unreported exception in load() " + exception);
14. }
15. } else {
16. doLoad();
17. }
18. }

最后调用了#doLoad()方法来具体的载入Session，源代码如下： 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

001. protected void doLoad() throws ClassNotFoundException, IOException {
002. if (log.isDebugEnabled())
003. log.debug("Start: Loading persisted sessions");
004. // 清空Map
005. sessions.clear();
006. // 对应work/Catalina/localhost/%app name%/SESSIONS.ser文件
007. File file = file();
008. if (file == null)
009. return;
010. if (log.isDebugEnabled())
011. log.debug(sm.getString("standardManager.loading", pathname));
012. FileInputStream fis = null;
013. ObjectInputStream ois = null;
014. Loader loader = null;
015. ClassLoader classLoader = null;
016. try {
017. // 载入Session缓存文件
018. fis = new FileInputStream(file.getAbsolutePath());
019. BufferedInputStream bis = new BufferedInputStream(fis);
020. if (container != null)
021. loader = container.getLoader();
022. if (loader != null)
023. classLoader = loader.getClassLoader();
024. if (classLoader != null) {
025. if (log.isDebugEnabled())
026. log.debug("Creating custom object input stream for class loader ");
027. ois = new CustomObjectInputStream(bis, classLoader);
028. } else {
029. if (log.isDebugEnabled())
030. log.debug("Creating standard object input stream");
031. ois = new ObjectInputStream(bis);
032. }
033. } catch (FileNotFoundException e) {
034. if (log.isDebugEnabled())
035. log.debug("No persisted data file found");
036. return;
037. } catch (IOException e) {
038. log.error(sm.getString("standardManager.loading.ioe", e), e);
039. if (ois != null) {
040. try {
041. ois.close();
042. } catch (IOException f) {
043. ;
044. }
045. ois = null;
046. }
047. throw e;
048. }
049. synchronized (sessions) {
050. try {
051. // 读出Session个数
052. Integer count = (Integer) ois.readObject();
053. int n = count.intValue();
054. if (log.isDebugEnabled())
055. log.debug("Loading " + n + " persisted sessions");
056. //  读入Session
057. for (int i = 0; i &lt; n; i++) {
058. StandardSession session = getNewSession();
059. session.readObjectData(ois);
060. session.setManager(this);
061. sessions.put(session.getIdInternal(), session);
062. session.activate();
063. sessionCounter++;
064. }
065. } catch (ClassNotFoundException e) {
066. log.error(sm.getString("standardManager.loading.cnfe", e), e);
067. if (ois != null) {
068. try {
069. ois.close();
070. } catch (IOException f) {
071. ;
072. }
073. ois = null;
074. }
075. throw e;
076. } catch (IOException e) {
077. log.error(sm.getString("standardManager.loading.ioe", e), e);
078. if (ois != null) {
079. try {
080. ois.close();
081. } catch (IOException f) {
082. ;
083. }
084. ois = null;
085. }
086. throw e;
087. } finally {
088. try {
089. if (ois != null)
090. ois.close();
091. } catch (IOException f) {
092. }
093. // 删除Session缓存文件
094. if (file != null && file.exists())
095. file.delete();
096. }
097. }
098. if (log.isDebugEnabled())
099. log.debug("Finish: Loading persisted sessions");
100. }

大致知道了Session的读取过程，后面就是Session没找到时创建Session的过程了。具体就是org.apache.catalina.session.StandardManager的#createSession()方法： 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

1. public Session createSession(String sessionId) {
2. if ((maxActiveSessions &gt;= 0) && (sessions.size() &gt;= maxActiveSessions)) {
3. rejectedSessions++;
4. throw new IllegalStateException(sm.getString("standardManager.createSession.ise"));
5. }
6. return (super.createSession(sessionId));
7. }

最后调用到了它的基类的#createSession()方法了。 

Java代码     [![收藏代码](http://zddava.iteye.com/images/icon_star.png)]()

01. public Session createSession(String sessionId) {
02. // 创建一个新的Session
03. Session session = createEmptySession();
04. // 初始化Session的属性
05. session.setNew(true);
06. session.setValid(true);
07. session.setCreationTime(System.currentTimeMillis());
08. session.setMaxInactiveInterval(this.maxInactiveInterval);
09. // 如果Session ID为null，那么就生成一个
10. if (sessionId == null) {
11. sessionId = generateSessionId();
12. }
13. session.setId(sessionId);
14. sessionCounter++;
15. return (session);
16. }

通过上述过程，一个新的Session就创建出来了。