色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

tomcat共享多個web應用會話的實現(xiàn)方法

瀏覽:127日期:2022-06-07 09:23:29

tomcat共享多個web應用會話的實現(xiàn)方法

問題

今天有位朋友問了個問題,大致是:tomcat下兩個Java web,一個是商城,一個是直播,從商城登錄后,再跳轉到直播,發(fā)現(xiàn)處于非登錄狀態(tài)。

解決思路

  1. 將session抽出來成一個session服務,統(tǒng)一通過該服務操作session。
  2. tomcat內部用會話管理器獲取會話時遍歷所有context內的會話。

方案1

重寫獲取session方法即可。

方案2

找了源碼發(fā)現(xiàn)已經(jīng)支持類似遍歷所有context內的會話的形式,首先獲取session時,如果cressContext屬性為true,則會在獲取不到時嘗試遍歷所有context是否存在該sessionid,如果存在則在本context根據(jù)sessionid創(chuàng)建自己的session對象。

 public HttpSession getSession(boolean create) {

    if (crossContext) {

      // There cannot be a session if no context has been assigned yet
      if (context == null)
return (null);

      // Return the current session if it exists and is valid
      if (session != null && session.isValid()) {
return (session.getSession());
      }

      HttpSession other = super.getSession(false);
      if (create && (other == null)) {
// First create a session in the first context: the problem is
// that the top level request is the only one which can 
// create the cookie safely
other = super.getSession(true);
      }
      if (other != null) {
Session localSession = null;
try {
  localSession =
    context.getManager().findSession(other.getId());
  if (localSession != null && !localSession.isValid()) {
    localSession = null;
  }
} catch (IOException e) {
  // Ignore
}
if (localSession == null && create) {
  localSession = 
    context.getManager().createSession(other.getId());
}
if (localSession != null) {
  localSession.access();
  session = localSession;
  return session.getSession();
}
      }
      return null;

    } else {
      return super.getSession(create);
    }

  }

context(web應用)獲取跨應用session時通過類似下面操作獲取:

request.getSession().getServletContext().getContext("/app2").getAttribute("att2"); 

這是因為request會根據(jù)cookies的sessionid獲取到session對象,這時不會報找不到,因為前面已經(jīng)根據(jù)其他sessionid創(chuàng)建了一個session對象,然后getContext操作會獲取對應url的context,接著進行會話操作。

public ServletContext getContext(String uri) {

    // Validate the format of the specified argument
    if (uri == null || !uri.startsWith("/")) {
      return null;
    }

    Context child = null;
    try {
      // Look for an exact match
      Container host = context.getParent();
      child = (Context) host.findChild(uri);

      // Non-running contexts should be ignored.
      if (child != null && !child.getState().isAvailable()) {
child = null;
      }

      // Remove any version information and use the mapper
      if (child == null) {
int i = uri.indexOf("##");
if (i > -1) {
  uri = uri.substring(0, i);
}
// Note: This could be more efficient with a dedicated Mapper
//    method but such an implementation would require some
//    refactoring of the Mapper to avoid copy/paste of
//    existing code.
MessageBytes hostMB = MessageBytes.newInstance();
hostMB.setString(host.getName());

MessageBytes pathMB = MessageBytes.newInstance();
pathMB.setString(uri);

MappingData mappingData = new MappingData();
((Engine) host.getParent()).getService().findConnectors()[0].getMapper().map(
    hostMB, pathMB, null, mappingData);
child = (Context) mappingData.context;
      }
    } catch (Throwable t) {
      ExceptionUtils.handleThrowable(t);
      return null;
    }

    if (child == null) {
      return null;
    }

    if (context.getCrossContext()) {
      // If crossContext is enabled, can always return the context
      return child.getServletContext();
    } else if (child == context) {
      // Can still return the current context
      return context.getServletContext();
    } else {
      // Nothing to return
      return null;
    }
  }

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家都對本站的支持!

標簽: JSP
主站蜘蛛池模板: 欧美成人免费网在线观看 | 亚洲经典三级 | 国产一级一片免费播放视频 | 狠狠综合久久 | 成人国产精品999视频 | 日韩中文字幕精品 | 日韩一级a毛片欧美一级 | 老司机免费福利午夜入口ae58 | 日本国产一区二区三区 | 欧美成人高清在线视频大全 | 国产福利不卡一区二区三区 | 狠狠澡夜夜澡人人爽 | 男人天堂网在线视频 | 午夜影院福利社 | 亚洲在线中文 | 欧美一级毛片在线播放 | 特级片免费看 | 波多野结衣在线观看一区二区三区 | 久久国产欧美日韩精品免费 | 久久国产精品-国产精品 | 成人区视频 | 久久精品免费全国观看国产 | 成人免费视频国产 | 午夜影院欧美 | 波多野结衣一级视频 | 手机看片日韩日韩韩 | 黄网在线观看免费网站台湾swag | 日本欧美一区二区三区不卡视频 | 久草欧美视频 | 日本三级日产三级国产三级 | 久久久久国产一级毛片高清板 | 91九色国产 | 手机看a | 亚洲精品98久久久久久中文字幕 | 色吊丝avav色吊丝 | 国产精品黄网站免费进入 | 欧美精品国产一区二区三区 | 手机看片日本 | 日韩中文字幕精品 | 国产毛片久久国产 | 日本xxxxx黄区免费看动漫 |