Invalid <url-pattern> /*.action in filter mapping error and (the url of Servlet and Filter matches url-p)

Servlet and filter are commonly used technologies in J2EE development. They are easy to use, simple to configure, and suitable for all ages.

  It is estimated that most of my friends use it directly for configuration and have not cared about the specific details. I encountered a problem today. I checked the servlet specification online and found that there are still some articles in the url-pattern in servlet and filter. I have some things and put them out for everyone's reference, so as not to waste time when encountering problems.

  First, the matching process of the servlet container to the url:

  When a request is sent to the servlet container, the container first takes the requested url minus the path of the current application context as the servlet mapping url. For example, I visit http://localhost/test/aaa.html, my application The context is test, the container will remove http://localhost/test, and the remaining /aaa.html part will be used for servlet mapping matching. This mapping matching process is sequential, and when a servlet is successfully matched, the remaining servlets will not be ignored (the filter is different, which will be mentioned later). The matching rules and order are as follows:

  1. Exact path matching. Example: 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 match the exact path and find that /test is exactly matched by servletA, then call servletA and ignore other servlets.

  2. The longest path matches. Example: The url-pattern of servletA is /test/*, and the url-pattern of servletB is /test/a/*. At this time, when accessing http://localhost/test/a, the container will select the servlet with the longest path. match, which is servletB here.

  3. Extension matching, if the last segment of the url contains an extension, the container will select the appropriate servlet according to the extension. Example: url-pattern of servletA: *.action

  4. If none of the previous three rules find a servlet, the container will select the corresponding request resource according to the url. If the application defines a default servlet, the container will drop the request to the default servlet (what is a default servlet? I'll talk about it later).

  According to this rule table, the matching process of the servlet can be clearly known, so the url-pattern should also be considered when defining the servlet to avoid errors.

  For filter, it will not match only one servlet like servlet, because the collection of filters is a chain, so only the order of processing is different, and there will be no processing order of selecting only one filter.Filter and filter-mapping in web. Same order as defined in xml.

  2. Detailed explanation of url-pattern

  In the web.xml file, the following syntax is used to define the mapping:

  Those starting with "/' and ending with "/*" are used for path mapping.

  Those starting with the prefix "*." are used for extended mappings.

  "is used to define the default servlet mapping.

  The rest is used to define the detailed mapping. For example: /aa/bb/cc.action

  So, why is it wrong to define "/*.action" for a seemingly normal match? Because this match belongs to both path mapping and extension mapping, the container cannot judge.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326504934&siteId=291194637