濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > tomcat共享多個(gè)web應(yīng)用會(huì)話的實(shí)現(xiàn)方法

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

熱門標(biāo)簽:外呼系統(tǒng)使用方法 電話機(jī)器人需要使用網(wǎng)絡(luò)嗎 給地圖標(biāo)注得傭金 海外圖書館地圖標(biāo)注點(diǎn) 南通通訊外呼系統(tǒng)產(chǎn)品介紹 潤(rùn)滑油銷售電銷機(jī)器人 如何看懂地圖標(biāo)注點(diǎn) 自繪地圖標(biāo)注數(shù)據(jù) 電銷機(jī)器人免培訓(xùn)

tomcat共享多個(gè)web應(yīng)用會(huì)話的實(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)部用會(huì)話管理器獲取會(huì)話時(shí)遍歷所有context內(nèi)的會(huì)話。

方案1

重寫獲取session方法即可。

方案2

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

 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í)通過類似下面操作獲?。?/p>

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

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

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ǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家都對(duì)本站的支持!

您可能感興趣的文章:
  • Tomcat實(shí)現(xiàn)session共享(session 會(huì)話復(fù)制)
  • 修改Tomcat服務(wù)器默認(rèn)端口號(hào)的實(shí)現(xiàn)方法
  • Centos7.3下Tomcat8的安裝配置教程
  • maven自動(dòng)部署到遠(yuǎn)程tomcat服務(wù)器的方法
  • Tomcat中的startup.bat原理詳細(xì)解析
  • Tomcat中的catalina.bat原理詳細(xì)解析

標(biāo)簽:樂山 黃石 貸款邀約 大連 銅川 內(nèi)江 南京 廣州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《tomcat共享多個(gè)web應(yīng)用會(huì)話的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  tomcat,共享,多個(gè),web,應(yīng)用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《tomcat共享多個(gè)web應(yīng)用會(huì)話的實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于tomcat共享多個(gè)web應(yīng)用會(huì)話的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    山丹县| 丹巴县| 海原县| 孟津县| 银川市| 嘉定区| 沂南县| 洛宁县| 怀仁县| 涞源县| 那曲县| 成武县| 龙江县| 洪泽县| 舟曲县| 连州市| 望谟县| 洪雅县| 西和县| 绿春县| 青浦区| 湖北省| 葫芦岛市| 永仁县| 深泽县| 松溪县| 醴陵市| 兴仁县| 湘西| 搜索| 江达县| 罗平县| 左云县| 四平市| 深水埗区| 寿宁县| 贵溪市| 怀来县| 贵定县| 镇远县| 疏勒县|