This article introduces the url-pattern matching rules of servlets and shares them with you. The details are as follows:
First, we need to clarify a few confusing rules:
1. The matching rules in the servlet container are neither simple wildcards nor regular expressions, but specific rules. So don't use wildcards or regular expression matching rules to view the url-pattern of a servlet.
2. Starting from Servlet 2.5, a servlet can use multiple url-pattern rules. The <servlet-mapping> tag declares the matching rules corresponding to the servlet, and each <url-pattern> tag represents 1 matching rule;
3. When the servlet container receives a url request initiated by the browser, the container will use the url to subtract the current application's context path and use the remaining string as the servlet mapping. If the url is http://localhost:8080/appDemo/index.html and its application context is appDemo, the container will remove http://localhost:8080/appDemo and use the remaining /index.html part to make the servlet mapping match.
4. The url-pattern mapping matching process has priority
5. And when a servlet matches successfully, the remaining servlets will not be paid attention to.
1. Four matching rules
1 Exact match
The items configured in <url-pattern> must match the url exactly.
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/user/users.html</url-pattern> <url-pattern>/index.html</url-pattern> <url-pattern>/user/addUser.action</url-pattern></servlet-mapping>
When entering the following URLs in the browser, they will be matched to the servlet
http://localhost:8080/appDemo/user/users.html
http://localhost:8080/appDemo/index.html
http://localhost:8080/appDemo/user/addUser.action
Notice:
http://localhost:8080/appDemo/user/addUser/ is an illegal url and will not be recognized as http://localhost:8080/appDemo/user/addUser
In addition, the above url can be followed by any query conditions, and will be matched, such as
http://localhost:8080/appDemo/user/addUser?username=Tom&age=23 will be matched to the MyServlet.
2 Path Match
A string that starts with the "/" character and ends with "/*" is used for path matching
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/user/*</url-pattern></servlet-mapping>
The path starts with /user/, and the following path can be arbitrary. For example, the following urls will be matched.
http://localhost:8080/appDemo/user/users.html
http://localhost:8080/appDemo/user/addUser.action
http://localhost:8080/appDemo/user/updateUser.actionl
3 extension matching
Strings starting with "*." are used for extension matching
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.action</url-pattern></servlet-mapping>
Then any url request with the extension jsp or action will match, for example, the following url will be matched
http://localhost:8080/appDemo/user/users.jsp
http://localhost:8080/appDemo/toHome.action
4 Default Match
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
2. Matching order
1. Exact match, servlet-mapping1: <url-pattern>/user/users.html</url-pattern>, servlet-mapping2: <url-pattern>/*</url-pattern>. When a request http://localhost:8080/appDemo/user/users.html comes, servlet-mapping1 matches and no longer matches with servlet-mapping2
2. Path matching, first match the longest path, then match the shortest path servlet-mapping1: <url-pattern>/user/*</url-pattern>, servlet-mapping2: <url-pattern>/*</url-pattern>. When a request http://localhost:8080/appDemo/user/users.html comes, servlet-mapping1 matches and no longer matches with servlet-mapping2
3. Extension matching, servlet-mapping1: <url-pattern>/user/*</url-pattern>, servlet-mapping2: <url-pattern>*.action</url-pattern>. When a request http://localhost:8080/appDemo/user/addUser.action comes, servlet-mapping1 matches and no longer matches with servlet-mapping2
4. The default match is not found in the above servlet, so use the default servlet and configure it as <url-pattern>/</url-pattern>
Issues to be noted
1 Path matching and extension matching cannot be set at the same time
There are only three matching methods, either path matching (starting with the "/" character and ending with the "/*"), extension matching (starting with the "*."), or exact matching. The three matching methods cannot be combined, so don't take it for granted to use wildcards or regular rules.
For example, <url-pattern>/user/*.action</url-pattern> is illegal
Also note: <url-pattern>/aa/*/bb</url-pattern> is an exact match and legal. * here is not the meaning of wild match.
2 "/*" and "/" have different meanings
Tomcat configures the default servlet in the %CATALINA_HOME%/conf/web.xml file, and the configuration code is as follows
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet><servlet> <servlet-name>jsp</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param> <param-name>fork</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>xpoweredBy</param-name> <param-value>false</param-value> </init-param> <load-on-startup>3</load-on-startup> </servlet><servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/</url-pattern></servlet-mapping> <!-- The mappings for the JSP servlet --><servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.jspx</url-pattern></servlet-mapping>
IV. Give an example
| Mapping URL | Corresponding Servlet |
| /hello | servlet1 |
| /bbs/admin/* | servlet2 |
| /bbs/* | servlet3 |
| *.jsp | servlet4 |
| / | servlet5 |
The result of the actual request mapping
Remove the remaining paths of the context path | Servlet that handles the request |
/hello | servlet1 |
/bbs/admin/login | servlet2 |
/bbs/admin/index.jsp | servlet2 |
/bbs/display | servlet3 |
/bbs/index.jsp | servlet3 |
/bbs | servlet3 |
/index.jsp | servler4 |
/hello/index.jsp | servlet4 |
/hello/index.html | servlet5 |
/news | servlet5 |
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.