Tomcat8.x web.xml过滤器的配置的解析->过滤器的实例化->过滤器的调用
// web.xml过滤器的配置的解析、过滤器的实例化、过滤器的调用
// web.xml过滤器的配置的解析、过滤器的实例化-------------1
class org.apache.catalina.core.StandardContext{
protected synchronized void startInternal() throws LifecycleException {
// 解析上下文的web.xml文件
fireLifecycleEvent(Lifecycle.CONFIGURE_START_EVENT, null); // 触发事件 "configure_start"-------
if (ok) {
// 启动过滤器,会把org.apache.tomcat.util.descriptor.web.FilterDef 转成对象 org.apache.catalina.core.ApplicationFilterConfig
if (!filterStart()) {
log.error(sm.getString("standardContext.filterFail"));
ok = false;
}
}
}
// 把org.apache.tomcat.util.descriptor.web.FilterDef 转成对象 org.apache.catalina.core.ApplicationFilterConfig
public boolean filterStart() {
// Instantiate and record a FilterConfig for each defined filter
boolean ok = true;
synchronized (filterConfigs) {
filterConfigs.clear();
for (Entry entry : filterDefs.entrySet()) {
String name = entry.getKey();
ApplicationFilterConfig filterConfig =
new ApplicationFilterConfig(this, entry.getValue());
filterConfigs.put(name, filterConfig);
}
}
return ok;
}
}
// 解析上下文的web.xml文件
class org.apache.catalina.startup.ContextConfig{
public void lifecycleEvent(LifecycleEvent event) {
context = (Context) event.getLifecycle(); // 取得触发者 org.apache.catalina.core.StandardContext
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) { // "configure_start"
configureStart();
}
}
// 配置启动事件 "configure_start"-------
protected synchronized void configureStart() {
// 读取并解析“d:/a/c/d/tomcat/conf/web.xml”,读取并解析"d:/a/b/tomcat/conf/Catalina/localhost/web.xml.default"
// 取得输入流 "d:/a/b/c/tomcat/webapps/dir1/WEB-INF/web.xml"
// 合并配置文件内容
// 合并全局配置
// 使用 org.apache.jasper.servlet.JspServlet 包装 /a/b/c/file.jsp 的文件
// 把解析处理的内容设置到 context 中 ,如:context.addFilterMap(filterMap);
webConfig();//!!!! 核心
}
protected void webConfig() {
WebXml webXml = createWebXml();
// Parse context level web.xml
// 取得输入流 "d:/a/b/c/tomcat/webapps/dir1/WEB-INF/web.xml"
InputSource contextWebXml = getContextWebXmlSource();
if (!webXmlParser.parseWebXml(contextWebXml, webXml, false)) { // 解析文件 ,数据存入webXml
ok = false;
}
// context === org.apache.catalina.core.StandardContext
configureContext(webXml); // 把解析处理的内容设置到 context 中 ,如:context.addFilterMap(filterMap);
}
//过滤器的配置信息
private void configureContext(WebXml webxml) {
// context === org.apache.catalina.core.StandardContext
for (FilterDef filter : webxml.getFilters().values()) { // 过滤器
if (filter.getAsyncSupported() == null) {
filter.setAsyncSupported("false");
}
context.addFilterDef(filter);
}
for (FilterMap filterMap : webxml.getFilterMappings()) { // 过滤器映射
context.addFilterMap(filterMap);
}
}
}
// 过滤器的调用-------------------2
class org.apache.catalina.core.StandardWrapperValve{
public final void invoke(Request request, Response response)
throws IOException, ServletException {
// ....
// !!! 创建过滤器链条
ApplicationFilterChain filterChain =
ApplicationFilterFactory.createFilterChain(request, wrapper, servlet);
// ....
// 执行过滤器链!!!!
filterChain.doFilter(request.getRequest(), response.getResponse());
}
}
// 过滤器链条
class org.apache.catalina.core.ApplicationFilterChain{
public static ApplicationFilterChain createFilterChain(ServletRequest request,
Wrapper wrapper, Servlet servlet) {
ApplicationFilterChain filterChain = null;
filterChain = (ApplicationFilterChain) req.getFilterChain();
if (filterChain == null) {
filterChain = new ApplicationFilterChain(); // 创建过滤器链
req.setFilterChain(filterChain);
}
filterChain.setServlet(servlet);
filterChain.setServletSupportsAsync(wrapper.isAsyncSupported());
// Acquire the filter mappings for this Context
StandardContext context = (StandardContext) wrapper.getParent();
FilterMap filterMaps[] = context.findFilterMaps(); //上下文的过滤器列表
for (int i = 0; i < filterMaps.length; i++) { // 添加过滤器,根据路径映射
if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
continue;
}
if (!matchFiltersURL(filterMaps[i], requestPath))
continue;
// 在org.apache.catalina.core.StandardContext.filterStart() 已经把
// context === org.apache.catalina.core.StandardContext
// filterConfig === org.apache.catalina.core.ApplicationFilterConfig
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
context.findFilterConfig(filterMaps[i].getFilterName());
if (filterConfig == null) {
// FIXME - log configuration problem
continue;
}
filterChain.addFilter(filterConfig);
}
// Add filters that match on servlet name second
for (int i = 0; i < filterMaps.length; i++) { // 添加过滤器,根据Servlet名称
if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
continue;
}
if (!matchFiltersServlet(filterMaps[i], servletName))
continue;
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
context.findFilterConfig(filterMaps[i].getFilterName());
if (filterConfig == null) {
// FIXME - log configuration problem
continue;
}
filterChain.addFilter(filterConfig);
}
// Return the completed filter chain
return filterChain;
}
// 执行过滤器链条
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
internalDoFilter(request,response);//!!!!
}
// 执行过滤器链条
private void internalDoFilter(ServletRequest request,
ServletResponse response)
throws IOException, ServletException {
if (pos < n) {
// org.apache.catalina.core.StandardContext.filterStart() 创建的ApplicationFilterConfig
ApplicationFilterConfig filterConfig = filters[pos++];
Filter filter = filterConfig.getFilter();
filter.doFilter(request, response, this);//!!!!执行过滤器
}
servlet.service(request, response); // 执行Servlet
}
}