jetty的servlet请求路由与ContextHandlerColleection的实现(三)
ts, i);
Object list = entry.getValue();
if (list instanceof Map) {
Map hosts = (Map)list;
String host = normalizeHostname(request.getServerName());
// explicitly-defined virtual hosts, most specific
list=hosts.get(host);
for (int j=0; j
Handler handler = (Handler)LazyList.get(list,j);
handler.handle(target,request, response, dispatch);
if (base_request.isHandled())
return;
}
// wildcard for one level of names
list=hosts.get("*."+host.substring(host.indexOf(".")+1));
for (int j=0; j
{
Handler handler = (Handler)LazyList.get(list,j);
handler.handle(target,request, response, dispatch);
if (base_request.isHandled())
return;
}
// no virtualhosts defined for the context, least specific
// will handle any request that does not match to a specific virtual host above
list=hosts.get("*");
for (int j=0; j
{
Handler handler = (Handler)LazyList.get(list,j);
handler.handle(target,request, response, dispatch);
if (base_request.isHandled())
return;
}
} else {
//一般都是执行这里,因为前面获取所有匹配的context的时候会将所有匹配上的handler构成一个数组
for (int j=0; j
Handler handler = (Handler)LazyList.get(list,j);
handler.handle(target,request, response, dispatch);
if (base_request.isHandled())
return; //如果有handler处理了,那么就不处理了
}
}
}
} else {
// This may not work in all circumstances... but then I think it should never be called
for (int i=0;i
handlers[i].handle(target,request, response, dispatch);
if ( base_request.isHandled())
return;
}
}
}
因为这里有了一些虚拟主机什么的东西,所以看起来稍微复杂一些。。但是其实最主要做的事情如下:
(1)调用当前contextMap的getLazyMatches方法,找到所有与当前匹配的contextHandle
(2)遍历这些handler,然后依次调用他们的handle方法来处理这个请求,知道这次的请求被处理了为止。。
好了,到这里ContextHandlerCollection的主要类容就算看完了。。。它还算是比较重要的。。。其实到这里为止http请求在jetty中的处理流程就算是比较的清楚了。。。用一张图来说明一下吧: