Content

tomcat共享多个web应用会话的实现方法(tomcat多个service)怎么可以错过

文章摘要

这篇文章介绍了如何通过`public ServletContext getContext(String uri)`的方法获取指定路径的上下文。该方法首先验证路径的有效性,如果路径为空或不以`/`开头则返回`null`。接着,它尝试通过容器查找匹配的子上下文。如果未找到子上下文且路径中包含`##`分隔符,会去掉分隔符并构造新的上下文。如果仍然未找到匹配结果,最后返回`null`。整个过程旨在高效地获取或构造所需上下文。

 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; } }