美文网首页工作生活
JavaEE通过源码了解Session的创建和获取

JavaEE通过源码了解Session的创建和获取

作者: 2019Say10 | 来源:发表于2019-07-02 22:51 被阅读0次

    四大作用域的使用范围:

    PageContext:
    在页面创建创建,只能当前页面使用,对象随着页面关闭而销毁,不能跨页面进行参数传递。
    RequestContext:
    RequestContetx:作用域为url请求,在发送连接请求时创建,在请求完毕后Request对象随之销毁。
    HttpSession:
    作用域为用户与服务器发送连接请求,直到游览器关闭。整个过程都只创建了一个Session(除了session过时),只能通过request.getSession创建session。

    RequestContext:
    在服务器启动时创建,作用域为整个应用,在服务器关闭时销毁对象。

    了解Session的创建

    在同一个页面多次获取session查看sessionid是否相同

     HttpSession session = request.getSession();
     HttpSession session1 = request.getSession();
     System.out.println(session.getId());
     System.out.println(session1.getId());
    

    得知在同一个页面内获取session为同一个Id

    打开另一个页面再次发送请求


    发现与之前使用的是同一session。
    切换游览器发送请求
    sessionId改变,在另一个游览器时发送请求时创建了另一个session.
    重启游览器发送请求*
    sessionId改变,在重启游览器再次发送请求后创建了新的session.
    结论
    在发送请求游览器没有关闭前,请求获取到的session都为同一session.在游览器切换(创建新的session)和重启游览器后(session销毁)创建了新的session.
    现在通过源码来看看session的创建和请求过程
    request.getSession的实现类为RequestFacade

    查看RequestFacade的getSession方法

    public HttpSession getSession(boolean create) {
        if (this.request == null) {
            throw new IllegalStateException(sm.getString("requestFacade.nullRequest"));
        } else {
            return SecurityUtil.isPackageProtectionEnabled() ? (HttpSession)AccessController.doPrivileged(new RequestFacade.GetSessionPrivilegedAction(create)) : this.request.getSession(create);
        }
    }
    

    当request为空时,抛出异常,不为空时通过判断的SecurityUtil.isPackageProtectionEnabled()是否存储过session,如果有获取获取session,没有则调用request.getSession(create)方法。这里我们主要看request里是如何获取session的。

    public HttpSession getSession(boolean create) {
          Session session = this.doGetSession(create);
          return session == null ? null : session.getSession();
      }
    

    再次调用了request里的doGetSession方法。

    protected Session doGetSession(boolean create) {
          Context context = this.getContext();
          if (context == null) {
              return null;
          } else {
              if (this.session != null && !this.session.isValid()) {
                  this.session = null;
              }
    
              if (this.session != null) {
                  return this.session;
              } else {
                  Manager manager = context.getManager();
                  if (manager == null) {
                      return null;
                  } else {
                      if (this.requestedSessionId != null) {
                          try {
                              this.session = manager.findSession(this.requestedSessionId);
                          } catch (IOException var13) {
                              this.session = null;
                          }
    
                          if (this.session != null && !this.session.isValid()) {
                              this.session = null;
                          }
    
                          if (this.session != null) {
                              this.session.access();
                              return this.session;
                          }
                      }
    
                      if (!create) {
                          return null;
                      } else {
                          boolean trackModesIncludesCookie = context.getServletContext().getEffectiveSessionTrackingModes().contains(SessionTrackingMode.COOKIE);
                          if (trackModesIncludesCookie && this.response.getResponse().isCommitted()) {
                              throw new IllegalStateException(sm.getString("coyoteRequest.sessionCreateCommitted"));
                          } else {
                              String sessionId = this.getRequestedSessionId();
                              if (!this.requestedSessionSSL) {
                                  if ("/".equals(context.getSessionCookiePath()) && this.isRequestedSessionIdFromCookie()) {
                                      if (context.getValidateClientProvidedNewSessionId()) {
                                          boolean found = false;
                                          Container[] var7 = this.getHost().findChildren();
                                          int var8 = var7.length;
    
                                          for(int var9 = 0; var9 < var8; ++var9) {
                                              Container container = var7[var9];
                                              Manager m = ((Context)container).getManager();
                                              if (m != null) {
                                                  try {
                                                      if (m.findSession(sessionId) != null) {
                                                          found = true;
                                                          break;
                                                      }
                                                  } catch (IOException var14) {
                                                  }
                                              }
                                          }
    
                                          if (!found) {
                                              sessionId = null;
                                          }
                                      }
                                  } else {
                                      sessionId = null;
                                  }
                              }
    
                              this.session = manager.createSession(sessionId);
                              if (this.session != null && trackModesIncludesCookie) {
                                  Cookie cookie = ApplicationSessionCookieConfig.createSessionCookie(context, this.session.getIdInternal(), this.isSecure());
                                  this.response.addSessionCookieInternal(cookie);
                              }
    
                              if (this.session == null) {
                                  return null;
                              } else {
                                  this.session.access();
                                  return this.session;
                              }
                          }
                      }
                  }
              }
          }
      }
    

    通过代码,分析session获取步骤如下

    1.在doGetSession方法里首先判断了当前context对象是否存在,如果存在则调用对session进行进一步判断。在session不为空但过时之后把session设置为null。如果session不为空则返回当前对象的session对象。
    2.如果当前对象的session为空,则通过session管理器manager获取session,如果有则返回session没有则进入下一步。
    3.通过requestId创建Session,在Manager.createSession(sessionId)方法中,如果sessionId存在则通过sessionid创建的session,否者重新构建sessionId创建session.
    4.session完成后把sessionId设置到Cookie中。
    5.通过access方法刷新session,最后返回session。

    相关文章

      网友评论

        本文标题:JavaEE通过源码了解Session的创建和获取

        本文链接:https://www.haomeiwen.com/subject/liyxhctx.html