The servlet url-pattern matching rules, including / and / distinction *

9. servlet url-pattern matching rule of

9.1 At the outset of confusion rule

Matching rules ① servlet container wildcard are neither simple nor regular expression, but certain rules

② Servlet 2.5 start, a servlet can use multiple url-pattern rules

③ When the servlet container receives a request url, the containers will be subtracted by the current application context path url, to the remaining string as servlet mapping

④ url-pattern matching process are mapped priority

⑤ and when there is a servlet after a successful match, they will not bother about the rest of the servlet

⑥ match url essence is to find the servlet, and the servlet generally used are the highest priority precisely match

⑦ If a request is not intercepted any servlet container, it does not need to servlet processing, direct access to resources

9.2 matching rules

① exact path match: requesting to specify a particular path processing method, the highest priority

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/user/users.html</url-pattern>
    <url-pattern>/index.html</url-pattern>
    <url-pattern>/user/addUser.action</url-pattern>
</servlet-mapping>

② path matching: the "/" character, and "/ *" matches the end of the string to the path, / * belongs to a particular path match. The second priority

[Note]
1. In the regular expression inside the / * on behalf of his previous character / have one or more

2. At the time of writing pointcut expressions. * Represents any object under the current level

3.url-pattern inside, on behalf of any content *

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/user/*</url-pattern>
</servlet-mapping>

③ extension matches: in. "*" At the beginning of the string is used to match the extension, the third priority

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

④ default match: The above can not find servlet, to use the default servlet, configure / lowest priority

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

9.3 Considerations

  1. Path matching and extension matching can not be set at the same time, as /user/*.action is illegal
  2. "/ " And "/" are not the same meaning:
    "/
    " is a path, and which can match all request, after an exact match due to the priority of the paths match, the "/ " will match to cover all extensions, many 404 are thereby causing errors, so this is a particularly bad match mode, the filter is generally used for pattern-URL
    "/" servlet is the default mode, and the mode is one and only one instance of the lowest priority does not cover any other url-pattern, but replaces the built-in default servlet servlet container, the same pattern will match any request.
    After configuring "/", a possible phenomenon is myServlet intercepts such as HTTP: // localhost: 8080 / appDemo / the User / addUser.action, HTTP: // localhost: format 8080 / appDemo / user / updateUser request but does not block http: // localhost: 8080 / appDemo / user / users.jsp, http: // localhost: 8080 / appDemo / index.jsp, because the servlet container has a built-in "
    .jsp" matcher, the extension matches a higher priority than the default match, why we have this phenomenon.

9.4 tomcat default servlet configuration

Tomcat Servlet default configuration in conf \ web.xml file, mainly to the end of the file that is jsp suffix extension matches the rule, 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>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

``
"/ *" And "/" will intercept load static resources, need special attention, including "/ *" will intercept the load jsp files, because the path to match a higher priority. .Jsp suffix match, that is the default jsp extension matches will fail, failure happens? It will not be intercepted. However, I configured / * represents the path match, the match is the root of all requests pathway, that is, the servlet intercepts all requests, including requests at the end of jsp. "/" Will not intercept jsp file requests, not that he will not block, but there is a higher priority than his first interception of the match extension. But the default matching resources to intercept static .html, .css, etc., because there is no allocation of resources to these static extension rules.

Note that this configuration is the servlet, that is, as long as the intercept, and on the implementation of servlet's service method for processing. However springmvc inside, intercepts all requests by a dispacherServlet, then matched to a specific method. The method of mapping an exact match are generally written, then the request to him to perform. DispacherServlet only responsible for intercepting requests, if the response of the mapping is not found, the default configuration if the processing of the request to put the controller servlet request referred to it, it does not respond to the mapping report 404. The default controller it will first determine the suffix meets my mapping rules (either a path mapping rules to match either the extension matches), if the match will come on a corresponding resource according to the path came to find is read back to the front end, I did not find happiness 404

<mvc:default-servlet-handler/>

JspServlet class is tamcat for all default servlet jsp configured to request the end of the current site, it can be intercepted request to the server, and then find the corresponding jsp return depending on the path coming, is a special request to access jsp default controller . In springMVC in, but also specifically configured to request access to the site of a static resource servlet for processing, the following configuration, you can use the default controller handles the request directly.

<mvc:resources mapping="/css/**" location="/css/"/> 
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/images/**" location="/images/"/>

You can also configure the native web.xml, all requests to the default org.apache.catalina.servlets.DefaultServlet tomcat to handle, only you need to do the following configuration:

<servlet-mapping> 
  <servlet-name>default</servlet-name>
  <url-pattern>*.jpg</url-pattern>  
</servlet-mapping> 
<servlet-mapping>   
  <servlet-name>default</servlet-name> 
  <url-pattern>*.js</url-pattern> 
</servlet-mapping> 
<servlet-mapping>   
  <servlet-name>default</servlet-name>   
  <url-pattern>*.css</url-pattern>  
</servlet-mapping>

Guess you like

Origin blog.csdn.net/superyayaya/article/details/93397812