Explain the URL pattern matching rules of servlets

This article introduces the URL pattern matching rules of servlets and shares them with you as follows:

First, we need to clarify several confusing rules:

1. The matching rules in the servlet container are neither simple generic matching nor regular expressions, but specific rules. So don't use wildcards or regular expression matching rules to look at servlet URL patterns.

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 one matching rule;

3. When the servlet container receives a URL request initiated by the browser, the container will subtract the context path of the current application from the URL, and take the remaining string as the servlet mapping, if the URL is http://localhost:8080/appDemo/index.html , the application context is appdemo, and the container will http://localhost:8080/appDemo Remove and use the remaining / index The HTML part is used for servlet mapping and matching

4. The URL pattern mapping matching process has priority

5. And when one servlet matches successfully, you won't pay attention to the remaining servlets.

1、 Four matching rules

1 exact match

The items configured in < URL pattern > must exactly match the URL.

When the following URLs are entered 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

be careful:

http://localhost:8080/appDemo/user/addUser/ Is an illegal URL and will not be treated as http://localhost:8080/appDemo/user/addUser distinguish

In addition, the above URL can be followed by any query criteria, which will be matched, such as

http://localhost:8080/appDemo/user/addUser?username=Tom&age=23 Will be matched to the myservlet.

2 path matching

Strings starting with "/" and ending with "/ *" are used for path matching

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

With "*." The first string is used for extension matching

Then any URL request with JSP or action extension will be matched. For example, the following URLs will be matched http://localhost:8080/appDemo/user/users.jsp http://localhost:8080/appDemo/toHome.action

4 default matching

2、 Matching order

1. Exact matching, servlet mapping1: < URL pattern > / user / users html,servlet-mapping2:
/*
。 When a request http://localhost:8080/appDemo/user/users.html When you come, servlet-mapping1 matches, and servlet-mapping2 no longer matches

2. Path matching: first the longest path, then 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 When you come, servlet-mapping1 matches, and servlet-mapping2 no longer matches

3. Extension matching, servlet mapping1: < URL pattern > / user / * < / url pattern >, servlet mapping2: < URL pattern > * action。 When a request http://localhost:8080/appDemo/user/addUser.action When you come, servlet-mapping1 matches, and servlet-mapping2 no longer matches

4. Default matching. If the servlet cannot be found above, use the default servlet and configure it as < URL pattern > / < / url pattern >

3、 Problems needing attention

1 path matching and extension matching cannot be set at the same time

There are only three matching methods, either path matching (starting with "/" character and ending with "/ *") or extension matching (starting with "*.") The three matching methods cannot be combined. Do not take wildcards or regular rules for granted.

For example, < URL pattern > / user / * Action < / url pattern > is illegal

In addition, note: < URL pattern > / AA / * / BB < / url pattern > is exact matching and legal. The * here does not mean universal matching

2 "/ *" and "/" have different meanings

Tomcat in% Catalina_ HOME%\conf\web. The default servlet is configured in the XML file. The configuration code is as follows

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>