国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

更多QQ空间微信QQ好友腾讯朋友复制链接
您的位置:首頁/技術文章
文章詳情頁

tomcat共享多個web應用會話的實現方法

【字号: 作者:豬豬瀏覽:97日期:2022-06-07 09:23:29

tomcat共享多個web應用會話的實現方法

問題

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

解決思路

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

方案1

重寫獲取session方法即可。

方案2

找了源碼發現已經支持類似遍歷所有context內的會話的形式,首先獲取session時,如果cressContext屬性為true,則會在獲取不到時嘗試遍歷所有context是否存在該sessionid,如果存在則在本context根據sessionid創建自己的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會根據cookies的sessionid獲取到session對象,這時不會報找不到,因為前面已經根據其他sessionid創建了一個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;
    }
  }

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

標簽: JSP
主站蜘蛛池模板: 欧美视频在线观看免费精品欧美视频 | 99福利网| 免费一级大毛片a一观看不卡 | 成年男女免费视频网站播放 | freexxxx性特大另类ww | 亚洲天堂手机在线 | 免费一级a毛片在线播放 | 亚洲男人在线 | 免费大片黄手机在线观看 | 日韩黄色毛片 | 色偷偷亚洲男人天堂 | 久久久精品国产免费观看同学 | 欧美另类丝袜 | 精品中文字幕在线观看 | 免费区欧美一级毛片 | 男人女人做性全程视视频 | 久久女厕一次看个够 | 国产欧美日韩综合精品一区二区三区 | 免费看的一级片 | 日本黄页网站免费大全 | 精品亚洲成a人片在线观看 精品亚洲成a人在线播放 | 亚洲美女高清aⅴ视频免费 亚洲美女黄色片 | 久久91亚洲精品中文字幕奶水 | 亚洲精品久久久久久久777 | 国产成人久久综合热 | yy6080久久亚洲精品 | 亚洲欧洲日产国码二区首页 | 在线播放亚洲精品 | 黄色网址在线免费观看 | 亚洲一区二区三区在线视频 | 香蕉香蕉国产片一级一级毛片 | 久久国产精品免费观看 | 国产91色综合久久免费 | 亚洲美女在线播放 | 精品国产一二三区在线影院 | 国产成人香蕉久久久久 | 美国三级在线 | 一级做a级爰片性色毛片视频 | 2级毛片| 亚洲国产成人精品91久久久 | 在线aaa |