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

随心笔谈9个月前发布 admin
192 00
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

文章摘要

这篇文章介绍了如何通过`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;
}
}

© 版权声明

相关文章