Servlet and filter are common technologies used in J2EE development. They are easy to use and easy to configure.
It is estimated that most of my friends are directly configured and used without having to worry about specific details. I encountered a problem today and found out only when I checked Servlet specifications online, the url-pattern in Servlet and filter are still included in some articles, which summarize some things and provide them for your reference to avoid wasting time in case of problems.
1. url matching process of Servlet containers:
When a request is sent to the Servlet container, the container first uses the requested url minus the current application context path as the Servlet ing url. For example, I access http: // localhost/test/aaa.html. My application context is test. The Container removes http: // localhost/test, and the rest of the/aaa.html part is used for Servlet ing matching. This ing matching process is ordered. When a Servlet matches successfully, the remaining Servlet filters will not be ignored, which will be mentioned later ). The matching rules and sequence are as follows:
1. Exact path matching. Example: for example, if the url-pattern of ServletA is/test and the url-pattern of ServletB is/*, if the url I access is http: // localhost/test, in this case, the container will first perform exact Path Matching and find that/test is exactly matched by ServletA. Then, the container will call ServletA and ignore other servlets.
2. Longest Path Matching. Example: The url-pattern of ServletA is/test/*, while the url-pattern of ServletB is/test/a/*. When you access http: // localhost/test/, the container will select the Servlet with the longest path to match, that is, the ServletB here.
3. Extension matching. If the last part of the url contains extensions, the container will select an appropriate Servlet based on the extension. Example: ServletA url-pattern: *. action
4. If no Servlet is found in the first three rules, the container selects the corresponding request Resource Based on the url. If the application defines a default Servlet, the container will throw the request to the default Servlet. What is the default Servlet? Later ).
According to this rule table, you can clearly understand the Servlet matching process. Therefore, when defining the Servlet, you must also consider the url-pattern method to avoid errors.
For a filter, it does not match only one Servlet as the Servlet does. Because the filter set is a chain, there will only be different processing sequence, and only one filter will not be selected. the processing sequence of Filter and filter-mapping are in the web. the Order Defined in xml is the same.
Ii. url-pattern explanation
In the web. xml file, the following syntax is used to define the ing:
It is used for path ing, starting with "/" and ending.
The extension ing starts.
"It is used to define the default Servlet ing.
The rest are used to define detailed mappings. For example:/aa/bb/cc. action
So why is an error when defining a seemingly normal match like "/*. action? Because this match is a path ing and also an extension ing, the Servlet container cannot be determined.
- Scala Servlet in Scala Language
- Servlet API Discussion
- Introduction to Servlet containers and Context
- Servlet Source file to Class Process
- Details about Listener listening to Http sessions