tomcat共享多個web應(yīng)用會話的實現(xiàn)方法
tomcat共享多個web應(yīng)用會話的實現(xiàn)方法
問題
今天有位朋友問了個問題,大致是:tomcat下兩個java web,一個是商城,一個是直播,從商城登錄后,再跳轉(zhuǎn)到直播,發(fā)現(xiàn)處于非登錄狀態(tài)。
解決思路
方案1
重寫獲取session方法即可。
方案2
找了源碼發(fā)現(xiàn)已經(jīng)支持類似遍歷所有context內(nèi)的會話的形式,首先獲取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應(yīng)用)獲取跨應(yīng)用session時通過類似下面操作獲?。?/p>
request.getsession().getservletcontext().getcontext("/app2").getattribute("att2");
這是因為request會根據(jù)cookies的sessionid獲取到session對象,這時不會報找不到,因為前面已經(jīng)根據(jù)其他sessionid創(chuàng)建了一個session對象,然后getcontext操作會獲取對應(yīng)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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家都對本站的支持!
相關(guān)文章
- jsp+servlet實現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計的實現(xiàn)方法
- 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內(nèi)置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎(chǔ)實踐
- JavaServlet的文件上傳和下載實現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法