您的位置:首页 > 运维架构 > Tomcat

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

2016-08-22 11:32 597 查看
在配置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方法,对于匹配规则,有优先级。
//Rule1--ExactMatchWrapper[]exactWrappers=contextVersion.exactWrappers;internalMapExactWrapper(exactWrappers,path,mappingData);//Rule2--PrefixMatchbooleancheckJspWelcomeFiles=false;Wrapper[]wildcardWrappers=contextVersion.wildcardWrappers;if(mappingData.wrapper==null){internalMapWildcardWrapper(wildcardWrappers,contextVersion.nesting,path,mappingData);.....}....//Rule3--ExtensionMatchWrapper[]extensionWrappers=contextVersion.extensionWrappers;if(mappingData.wrapper==null&&!checkJspWelcomeFiles){internalMapExtensionWrapper(extensionWrappers,path,mappingData,true);}//Rule4--Welcomeresourcesprocessingforservletsif(mappingData.wrapper==null){booleancheckWelcomeFiles=checkJspWelcomeFiles;if(!checkWelcomeFiles){char[]buf=path.getBuffer();checkWelcomeFiles=(buf[pathEnd-1]=='/');}if(checkWelcomeFiles){for(inti=0;(i<contextVersion.welcomeResources.length)&&(mappingData.wrapper==null);i++){...//Rule4a--WelcomeresourcesprocessingforexactmacthinternalMapExactWrapper(exactWrappers,path,mappingData);//Rule4b--Welcomeresourcesprocessingforprefixmatchif(mappingData.wrapper==null){internalMapWildcardWrapper(wildcardWrappers,contextVersion.nesting,path,mappingData);}//Rule4c--Welcomeresourcesprocessing//forphysicalfolderif(mappingData.wrapper==null&&contextVersion.resources!=null){Objectfile=null;StringpathStr=path.toString();try{file=contextVersion.resources.lookup(pathStr);}catch(NamingExceptionnex){//Swallownotfound,sincethisisnormal}if(file!=null&&!(fileinstanceofDirContext)){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);}}/*welcomefileprocessing-take2*Nowthatwehavelookedforwelcomefileswithaphysical*backing,nowlookforanextensionmappinglisted*butmaynothaveaphysicalbackingtoit.Thisisfor*thecaseofindex.jsf,index.do,etc.*Awatereddownversionofrule4*/if(mappingData.wrapper==null){booleancheckWelcomeFiles=checkJspWelcomeFiles;if(!checkWelcomeFiles){char[]buf=path.getBuffer();checkWelcomeFiles=(buf[pathEnd-1]=='/');}if(checkWelcomeFiles){for(inti=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);}}//Rule7--Defaultservletif(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

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomcat servlet