Url-pattern matching rules for Servlets

Source: Internet
Author: User

About ContextPath:

Https://www.cnblogs.com/java-gwd/p/6495457.html

50,546,204 First you need to clarify a few confusing rules:
    1. A matching rule in a servlet container is neither a simple wildcard nor a regular expression, but a specific rule. So don't use wildcard characters or regular expression matching rules to see the Url-pattern of Servlets.
    2. Beginning with servlet 2.5, a servlet can use multiple Url-pattern rules,<servlet-mapping> tags to declare matching rules corresponding to the servlet, each <url-pattern> The label represents 1 matching rules;
    3. When the servlet container receives a URL request initiated by the browser, the container subtracts the current app's context path with the URL, using the remaining string as the servlet map, if the URL is http://localhost:8080/appDemo/ Index.html, the application context is Appdemo, the container will http://localhost:8080/appDemo removed, with the rest of the/index.html part to do the servlet mapping match
    4. Url-pattern mapping matching process is prioritized
    5. And when a servlet match succeeds, it won't bother with the rest of the servlet.
One or four matching rules 1 Exact Match

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

<Servlet-mapping><Servlet-name>myservlet</Servlet-name> <url-pattern< Span style= "COLOR: #0000ff" >>/user/users.html</ Url-pattern> < Url-pattern>/index.html</ url-pattern> << Span style= "COLOR: #800000" >url-pattern>/user/adduser.action </url-pattern>< Span style= "COLOR: #0000ff" ></servlet-mapping>  

When several URLs are entered in the browser, they are matched to the servlet
Http://localhost:8080/appDemo/user/users.html
Http://localhost:8080/appDemo/index.html
Http://localhost:8080/appDemo/user/addUser.action

  

Attention:

http://localhost:8080/appDemo/user/addUser/is an illegal URL and will not be recognized as a http://localhost:8080/appDemo/user/addUser

In addition, the above URL can be followed by arbitrary query conditions, will be matched, such as

Http://localhost:8080/appDemo/user/addUser?username=Tom&age=23 will be matched to Myservlet.

2 Path Matching

  A string that starts with a "/" character and ends with "/*" for path matching

  

 <servlet-mapping > <servlet-name< Span style= "COLOR: #0000ff" >>myservlet</ Servlet-name> < Url-pattern>/user/*</ url-pattern></< Span style= "COLOR: #800000" >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 matches

With "*." The string at the beginning is used for the extension match

<servlet-mapping>    <servlet-name>MyServlet</servlet-name>    <url-pattern>*.jsp </url-pattern>    <url-pattern>*.action</url-pattern></servlet-mapping>

Any URL requests that have an extension of JSP or action will match, such as the following URLs 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> 

Second, the 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, no longer matches with Servlet-mapping2
    2. Path matching, first longest path match, and then shortest path match 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, no longer matches with Servlet-mapping2
    3. Extension matches, 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, no longer matches with Servlet-mapping2
    4. Default match, no servlet found above, with default servlet, configured as <url-pattern>/</url-pattern>
Iii. issues needing attention 1 path matching and extension matching cannot be set at the same time

There are only three matching methods, either path matching ( beginning with the "/" character and ending with "/*" ) or extension matching ( "*."). Start ), either exact match, three matching methods cannot be combined, do not assume that wildcards or regular rules are used.

such as <url-pattern>/user/*.action</url-pattern> is illegal

Also note that:<url-pattern>/aa/*/bb</url-pattern> is precisely matched, legal, and here the * is not the meaning of the wildcard

2 "/*" and "/" do not have the same meaning
    • "/*" is a path match and can match all request, because the path match priority is second only to the exact match, so "/*" overrides all extension matches, many 404 errors are caused, so this is a particularly bad match pattern, Generally only for filter url-pattern
    • "/" is a special matching pattern in the servlet that has only one instance of the pattern, has the lowest priority, does not overwrite any other url-pattern, but replaces the built-in default servlet of the Servlet container, which also matches all request.
    • After configuring "/", one possible phenomenon is that myservlet will intercept such as Http://localhost:8080/appDemo/user/addUser.action, http://localhost:8080/appDemo/ User/updateuser in the format of the request, but does not intercept http://localhost:8080/appDemo/user/users.jsp, http://localhost:8080/appDemo/ Index.jsp, this is because the servlet container should have a built-in "*.jsp" match, and the extension match has a higher precedence than the default match.

Tomcat configures the default servlet in the%catalina_home%\conf\web.xml file, with the following configuration code

  

<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< Span style= "COLOR: #0000ff" >>jsp</servlet-name< Span style= "COLOR: #0000ff" >> <url-pattern>*.jsp</url-pattern> <url-pattern>*.jspx</url-pattern></servlet-mapping>             

    • Can read Http://stackoverflow.com/questions/4140448/difference-between-and-in-servlet-mapping-url-pattern
    • "/*" and "/" will block the loading of static resources, requiring special attention

Iv. Examples of
The mapped URL The corresponding servlet
/hello Servlet1
/bbs/admin/* Servlet2
/bbs/* Servlet3
*.jsp Servlet4
/ Servlet5

Results of Actual request mappings

Remove the remaining path 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

Url-pattern matching rules for Servlets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.