午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

tomcat共享多個(gè)web應(yīng)用會話的實(shí)現(xiàn)方法

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

tomcat共享多個(gè)web應(yīng)用會話的實(shí)現(xiàn)方法

問題

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

解決思路

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

方案1

重寫獲取session方法即可。

方案2

找了源碼發(fā)現(xiàn)已經(jīng)支持類似遍歷所有context內(nèi)的會話的形式,首先獲取session時(shí),如果cressContext屬性為true,則會在獲取不到時(shí)嘗試遍歷所有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應(yīng)用)獲取跨應(yīng)用session時(shí)通過類似下面操作獲取:

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

這是因?yàn)閞equest會根據(jù)cookies的sessionid獲取到session對象,這時(shí)不會報(bào)找不到,因?yàn)榍懊嬉呀?jīng)根據(jù)其他sessionid創(chuàng)建了一個(gè)session對象,然后getContext操作會獲取對應(yīng)url的context,接著進(jìn)行會話操作。

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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家都對本站的支持!

標(biāo)簽: JSP
主站蜘蛛池模板: 激情六月综合 | 久久中文字 | 久久精品黄色片 | 日韩有码第一页 | 日本久久久久久久久 | 蜜臀久久99精品久久久无需会员 | 久久黄色网址 | 亚洲免费精品视频 | 国产aaa级片 | 精品999视频 | 欧美成人精品欧美一级乱黄 | 91精品久久久久久粉嫩 | 一级中国毛片 | 少妇性l交大片7724com | 福利一区三区 | 精品国产一区二区三区久久久蜜臀 | xxx在线播放 | 欧美一级欧美三级 | 91麻豆免费 | 香蕉视频在线观看视频 | 中文字幕精品在线 | 手机看片日韩在线 | 国产精品免费在线视频 | 99久久精品国产一区二区成人 | 成人激情视频在线 | 亚洲天堂2016 | 成人小视频免费观看 | 国产区视频 | 免费av在线播放 | 免费在线观看www | 白嫩白嫩国产精品 | 亚洲欧美日韩综合在线 | 看黄网站在线观看 | 国产精品成人一区二区网站软件 | 黄色在线一区 | 欧美成人一区二区三区 | 四虎国产精品永久在线国在线 | 国产v片在线观看 | 99久久精品国产成人一区二区 | 亚州av在线播放 | 国产首页 |