When configuring web.xml for tomcat, servlet is a relatively important issue. Here we discuss several pain points in servlet.
1 servlet There are three matching modes for 1.1 Exact Match The items configured in Code example: point_down: <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/kata/detail.html</url-pattern> <url-pattern>/demo.html</url-pattern> <url-pattern>/table</url-pattern> </servlet-mapping> When you enter the following URLs in the browser, they will be matched to the servlet Notice: In addition, the above URL can be followed by any query conditions, which will be matched, such as The request 1.2 Path Matching A string that starts with a "/" character and ends with "/*" is used for path matching. Code example: point_down: <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.action</url-pattern> </servlet-mapping> The path starts with /user/ and the rest of the path can be anything. For example, the following URLs will be matched. 1.3 Suffix Matching Strings starting with "*." are used for suffix matching. Code example: point_down: <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 URLs will be matched Note: Path and suffix matching cannot be set at the same time Note: Path and extension matching cannot be set at the same time, such as the following three A few examples: point_down:, if you don't understand, please read Chapter 3 of this article 2 The difference between Let’s talk about Besides 3 URL-pattern priority issues When a URL matches the matching rules of multiple servlets, the corresponding servlet is matched according to the priority of "exact path > longest path > suffix match". Example 1: For example, the url-pattern of servletA is /test, and the url-pattern of servletB is /*. At this time, if the url I visit is http://localhost/test, the container will first perform an exact path match and find that /test is exactly matched by servletA. In this case, servletA will be called without caring about servletB. Example 2: For example, if the url-pattern of servletA is /test/ and the url-pattern of servletB is /test/a/ , when you access http://localhost/test/a, the container will select the servlet with the longest path to match, which is servletB here. Example 3: For example, the url-pattern of servletA is: *.action, and the url-pattern of servletB is Then a question arises. Why does The reason is very simple. There will be the following configuration in tomcat/conf/web.xml <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> :point_up_2: It can be clearly seen that 4 Root path You may have noticed a problem, that is, when the url-pattern is /*, accessing http://localhost:8080/ will result in 404, but accessing http://localhost:8080/index.html will work fine (of course, the premise is that First of all, we must be clear about what the root directory of a URL, i.e. / (such as http://localhost:8080/), means? Through experiments, we found that / is very special. It will be matched by the url-pattern of /*, but it will not be matched by the url-pattern of /. 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 and not by defaultservlet. The following snippet found in the tomcat source code can support my view: point_down:
:point_up_2:The above is about Translation:point_down: When the request URI points to a directory, the default servlet looks for a "welcome file" in that directory and, if present, at the corresponding resource URI to display. If no welcome file exists, the default servlet either provides a directory listing (see the default servlet configuration for how to customize it) or returns a 404 status, depending on the value of the listing setting. The reason why / is redirected to the welcome page is the existence of Summarize This is the end of this article about the detailed explanation of the problem of configuring servlet url-pattern in tomcat. For more relevant content about tomcat configuration servlet, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The front-end must know how to lazy load images (three methods)
Table of contents Preface 1. Deployment and Confi...
<body> <div id="root"> <...
Docker officially recommends that we use port map...
When using HTML tables, we sometimes need to chan...
This article example shares the specific code of ...
1. Normal background blur Code: <Style> htm...
In the previous article, I introduced how to solv...
This article shares with you how to install the M...
This article discusses several major zero-copy te...
Table of contents What is nodejs Install NodeJS H...
I joined a new company these two days. The compan...
Anyone who has used the Linux system should know ...
The docker image id is unique and can physically ...
Generally speaking, it is unlikely that you will ...
question Nginx takes $remote_addr as the real IP ...