Tomcat中web.xml中url-pattern的配置详解

来源:互联网 发布:360mac版下线 编辑:程序博客网 时间:2024/06/06 03:17

在配置tomcat的web.xml文件的时候,对于servlet的匹配,通常会有以下几种写法:

 <url-pattern>/exact.do</url-pattern>
 
 <url-pattern>/*</url-pattern>
<url-pattern>/</url-pattern>

那这几种写法有何不同呢?

主要是tomcat在启动的时候会扫描web.xml文件,然后得到servlet的映射数据servletMappings,然后会调用Context(实现类为StandardContext)
的addServletMapping方法。


路径会分为:

 1)以 /* 结尾的。 path.endsWith("/*"), 对应的Servlet会被丢到wildcardWrappers中

 2) 以 *. 开头的。 path.startsWith("*."),会被丢到extensionWrappers中

 3)是否是 /。      path.equals("/"),/ 会被丢到defaultWrapper中

 4) 以上3种之外的。 其他的映射都被丢到exactWrappers中

用户请求过来的时候会调用mapper的internalMapWrapper方法,对于匹配规则,有优先级。

// Rule 1 -- Exact Match        Wrapper[] exactWrappers = contextVersion.exactWrappers;        internalMapExactWrapper(exactWrappers, path, mappingData);        // Rule 2 -- Prefix Match        boolean checkJspWelcomeFiles = false;        Wrapper[] wildcardWrappers = contextVersion.wildcardWrappers;        if (mappingData.wrapper == null) {            internalMapWildcardWrapper(wildcardWrappers, contextVersion.nesting,                                       path, mappingData);            .....        }        ....// Rule 3 -- Extension Match        Wrapper[] extensionWrappers = contextVersion.extensionWrappers;        if (mappingData.wrapper == null && !checkJspWelcomeFiles) {            internalMapExtensionWrapper(extensionWrappers, path, mappingData,                    true);        }        // Rule 4 -- Welcome resources processing for servlets        if (mappingData.wrapper == null) {            boolean checkWelcomeFiles = checkJspWelcomeFiles;            if (!checkWelcomeFiles) {                char[] buf = path.getBuffer();                checkWelcomeFiles = (buf[pathEnd - 1] == '/');            }            if (checkWelcomeFiles) {                for (int i = 0; (i < contextVersion.welcomeResources.length)                         && (mappingData.wrapper == null); i++) {                    ...// Rule 4a -- Welcome resources processing for exact macth                    internalMapExactWrapper(exactWrappers, path, mappingData);                    // Rule 4b -- Welcome resources processing for prefix match                    if (mappingData.wrapper == null) {                        internalMapWildcardWrapper                            (wildcardWrappers, contextVersion.nesting,                             path, mappingData);                    }                    // Rule 4c -- Welcome resources processing                    //            for physical folder                    if (mappingData.wrapper == null                        && contextVersion.resources != null) {                        Object file = null;                        String pathStr = path.toString();                        try {                            file = contextVersion.resources.lookup(pathStr);                        } catch(NamingException nex) {                            // Swallow not found, since this is normal                        }                        if (file != null && !(file instanceof DirContext) ) {                            internalMapExtensionWrapper(extensionWrappers, path,                                                        mappingData, true);                            if (mappingData.wrapper == null                                && contextVersion.defaultWrapper != null) {                                mappingData.wrapper =                                    contextVersion.defaultWrapper.object;                                mappingData.requestPath.setChars                                    (path.getBuffer(), path.getStart(),                                     path.getLength());                                mappingData.wrapperPath.setChars                                    (path.getBuffer(), path.getStart(),                                     path.getLength());                                mappingData.requestPath.setString(pathStr);                                mappingData.wrapperPath.setString(pathStr);                            }                        }                    }                }                path.setOffset(servletPath);                path.setEnd(pathEnd);            }        }        /* welcome file processing - take 2         * Now that we have looked for welcome files with a physical         * backing, now look for an extension mapping listed         * but may not have a physical backing to it. This is for         * the case of index.jsf, index.do, etc.         * A watered down version of rule 4         */        if (mappingData.wrapper == null) {            boolean checkWelcomeFiles = checkJspWelcomeFiles;            if (!checkWelcomeFiles) {                char[] buf = path.getBuffer();                checkWelcomeFiles = (buf[pathEnd - 1] == '/');            }            if (checkWelcomeFiles) {                for (int i = 0; (i < contextVersion.welcomeResources.length)                         && (mappingData.wrapper == null); i++) {                    path.setOffset(pathOffset);                    path.setEnd(pathEnd);                    path.append(contextVersion.welcomeResources[i], 0,                                contextVersion.welcomeResources[i].length());                    path.setOffset(servletPath);                    internalMapExtensionWrapper(extensionWrappers, path,                                                mappingData, false);                }                path.setOffset(servletPath);                path.setEnd(pathEnd);            }        }        // Rule 7 -- Default servlet        if (mappingData.wrapper == null && !checkJspWelcomeFiles) {            if (contextVersion.defaultWrapper != null) {                mappingData.wrapper = contextVersion.defaultWrapper.object;                mappingData.requestPath.setChars                    (path.getBuffer(), path.getStart(), path.getLength());                mappingData.wrapperPath.setChars                    (path.getBuffer(), path.getStart(), path.getLength());            }            ...        }

结论:“/”的优先级最低,所以资源基本会被匹配到。而“/*”优先级较高,容易出现404错误。

备注:文章内容部分来自:http://www.cnblogs.com/fangjian0423/p/servletContainer-tomcat-urlPattern.html#springmvc


0 0