In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article is mainly about solving the problem of tomcat on the configuration of servlet url-pattern, the content is clear, interested friends can learn, I believe it will be helpful after reading.
When tomcat is configuring web.xml, servlet is an important issue. Here we will discuss several pain points in servlet.
Servlet url-pattern matching problem the difference between / and / * in url-pattern the priority problem of url-pattern the matching problem of root path /
Matching problem of 1 servlet url-pattern
There are three matching patterns in url-pattern: path matching, exact matching and suffix matching.
1.1 exact match
The items configured in must exactly match the url.
Code example: point_down:
MyServlet / kata/detail.html / demo.html / table
When the following url is entered in the browser, it will be matched to the servlet
Http://10.43.11.143/myapp/kata/detail.html
Http://10.43.11.143/myapp/demo.html
Http://10.43.11.143/myapp/table
Note:
Http://10.43.11.143/myapp/table/ is an illegal url and will not be recognized as http://10.43.11.143/myapp/table
In addition, the above url can be followed by any query condition, which will be matched, such as
The http://10.43.11.143/myapp/table?hello request will be matched to MyServlet.
1.2 path matching
A string that begins with the "/" character and ends with "/ *" is used for path matching
Code example: point_down:
MyServlet * .jsp * .action
The path starts with / user/, and the subsequent path can be arbitrary. For example, the following url will be matched.
Http://localhost:8080/appDemo/user/users.html
Http://localhost:8080/appDemo/user/addUser.action
Http://localhost:8080/appDemo/user/updateUser.actionl
1.3 suffix matching
With "*." The beginning string is used for suffix matching
Code example: point_down:
MyServlet * .jsp * .action
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
Note: path and suffix matching cannot be set at the same time
Note: path and extension matching cannot be set at the same time, for example, the following three are illegal. If set, starting the tomcat server will report an error.
/ kata/*.jsp
/ * .jsp
He*.jsp
A few examples: point_down:, does not understand. Please see Chapter 3 of this article.
2 the difference between / and / * in url-pattern
/
/ *
First of all, / *, / * is relatively easy to understand, it is a kind of path matching, in terms of scope, it is the broadest range of path matching, all requests meet its requirements, in terms of accuracy, it is the lowest precision path matching (note! I am talking about path matching), the priority of path matching is from long to short (see Chapter 3 of this article), so it is the least accurate path matching. Many blogs say that it is characterized by matching *. Jsp, isn't that nonsense? / * itself is a path match, and of course it can match * .jsp.
In addition, /, / is the match with the lowest priority of matching. when a url does not match all the url-pattern, the url will go / match and there is no restriction of * .jsp at all. The reason why people produce (and objectively it is) / will not match * .jsp but / * will match * .jsp is that the configuration of * .jsp is configured separately in tomcat/conf/web.xml. For more information, please see Chapter 3 of this article.
3 priority of url-pattern
When the matching rules of a url and multiple servlet can match, the corresponding servlet is matched according to the priority of "exact path > longest path > suffix matching".
Example 1: for example, the url-pattern of servletA is / test,servletB and the url-pattern is / *. At this time, if the url I visit is http://localhost/test, the container will first perform exact path matching and find that / test is exactly matched by servletA, then call servletA instead of servletB.
Example 2: for example, the url-pattern of servletA is / test/ and the url-pattern of servletB is / test/a/. When accessing http://localhost/test/a, the container will select the servlet with the longest path to match, that is, the servletB here.
Example 3: for example, the url-pattern:*.action of servletA and the url-pattern of servletB is / *. At this time, if the url I visit is http://localhost/test.action, the container will give priority to path matching instead of matching the extension, so call servletB.
Then there is a question. Why does / * match to * .jsp, but / doesn't match to * .jsp?
The reason is simple: there will be the following configuration in tomcat/conf/web.xml
Default / jsp * .jsp * .jspx
: point_up_2: you can clearly see that * .jsp matches as the suffix of the servlet named jsp. / * is the path match, and its priority is higher than the suffix match, so it can match the file with the suffix jsp. And / is the lowest-level match, which is lower than the suffix match, so the jsp file will not be matched by url-pattern /.
The matching problem of 4 root paths /
You should notice that when url-pattern is / *, accessing http://localhost:8080/ will be 404, but accessing http://localhost:8080/index.html will not be a problem (of course, if it is configured in the spring container). When url-pattern is /, http://localhost:8080/ is automatically forwarded to http://localhost:8080/index.html instead of 404. What is the reason?
First of all, we have to be clear: what does the root directory of a URL, such as http://localhost:8080/, mean? Through the experiment, it is found that / is very special, it will be matched by url-pattern as / *, but it will not be matched by url-pattern.
In tomcat, / is matched by defaultservlet by default, but its priority is lower than path matching, so when the url-pattern of a servlet is / *, / will be matched by this servlet, so it will not be matched by defaultservlet.
Find the following snippet in the tomcat source code to support my view: point_down:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.