Servlet: mainly used for processing a request coming from the client, and returns a response . A data acquisition request> Request Processing> completion response
Process: The client sends a request ---- HTTP server receives the request, HTTP server is only responsible for resolving static HTML interface, which contains the request forwarded to the Servlet Servlet container ----- container again according to the mapping relational mapping web.xml to create Servlet object (converted to HttpServletxxx objects ), then call processing corresponding to the Servlet --- results returned to the Servlet container, and then transferred to the client via the HTTP server.
Servlet life cycle:
Initialization Phase : Servlet Servlet container after creating object calls init () method , the method receives Servletconfig parameters, configuration information of the container through an object Servlet initialization parameter, and only a call once.
Run Phase: When receiving a request Servlet container object, and the ServletRequest ServletResponse objects created, call service () method , the object through the front two processes the request and response. Multiple calls.
Destruction Stage: Server shut down or Servlet that is invoked when an object is removed the destroy () , is called only once.
First need to clear a few confusing rules:
- matching rules servlet container is neither simple wildcard, nor is a regular expression, but certain rules . So do not use wildcards or regular expression matching rules to look at the servlet url-pattern .
- Start Servlet 2.5, a servlet may be used a plurality of rules url-pattern , <servlet-mapping> tag corresponding matching rule declares the servlet, each <url-pattern> tag represents a matching rule;
- When the servlet container receives a browser request url initiated, the container will be subtracted from the current application context path url to the remaining string as servlet mapping, if the url is http: // localhost: 8080 / appDemo / index. html, application context is appDemo, the container will be http: // localhost: 8080 / appDemo removed, with the remaining /index.html portion used to do the mapping to match the servlet
- url-pattern matching process are mapped priority
- And when there is a servlet after a successful match, it will not bother about the rest of the servlet.
First, four kinds of matching rules
1 exact match
Item <url-pattern> must exactly match the configuration completely url.
<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>
When the input are several url in the browser, which will be matched to the servlet
HTTP: // localhost: 8080 / appDemo / User / users.html
HTTP: // localhost: 8080 / appDemo / index.html
HTTP: // localhost: 8080 / appDemo / user / addUser.action
note:
http: // localhost: 8080 / appDemo / user / addUser / illegal url, will not be treated as http: // localhost: 8080 / appDemo / user / addUser recognition
Also with the above-described rear url can query any, it will be matched, such as
http: // localhost: 8080 / appDemo / user / addUser username = Tom & age = 23 will be matched to MyServlet?.
2 match path
The "/" character, and "/ *" matches the end of the string path for
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping>
Path / user / start, the latter path can be arbitrary. For example, the following url will be matched.
HTTP: // localhost: 8080 / appDemo / the User / users.html
HTTP: // localhost: 8080 / appDemo / the User / addUser.action
HTTP: // localhost: 8080 / appDemo / the User / updateUser.actionl
3 extension matches
With "*." Beginning of the string is used for extension matches
<servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.action</url-pattern> </servlet-mapping>
Url name extension jsp then any action or request are matched, such as the following will be matched url
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, matching order
- Exact match, servlet-mapping1: <url-pattern> /user/users.html </ url-pattern>, servlet-mapping2: <url-pattern> / * </ url-pattern>. When a request for http: // localhost: 8080 / appDemo / user / users.html comes, servlet-mapping1 to match, with no matching servlet-mapping2
- Matching path, the longest path to match, then the shortest path to match servlet-mapping1: <url-pattern> / user / * </ url-pattern>, servlet-mapping2: <url-pattern> / * </ url-pattern> . When a request for http: // localhost: 8080 / appDemo / user / users.html comes, servlet-mapping1 to match, with no matching servlet-mapping2
- Extension matches, servlet-mapping1: <url-pattern> / user / * </ url-pattern>, servlet-mapping2:. <Url-pattern> * action </ url-pattern>. When a request for http: // localhost: When 8080 / appDemo / user / addUser.action come, servlet-mapping1 to match, with no matching servlet-mapping2
- Default match can not be found above the servlet, the servlet to use the default, configured to <url-pattern> / </ url-pattern>
Third, the problems that need attention
1 matching path and extension matches can not be set at the same time
Only three matching method, either path match ( with a "/" character, and "/ *" at the end ), either the extension matches ( with "*." At the beginning ), or an exact match three matching method can not be combined, do not assume the use of wild cards or regular rules.
The <url-pattern> / user / *. Action </ url-pattern> is illegal
Also note: <url-pattern> / aa / * / bb </ url-pattern> is an exact match, legal, meaning here are not wildcarded *
2 "/ *" and "/" is not the same meaning
- "/ *" Is a path matching, and can match all request, because the priority second only to exactly match the path matches, so "/ *" will cover all the extension matches, a lot of 404 errors are caused thereby, so this is a particularly bad match mode, the filter is generally used url-pattern
- "/" Is a special servlet matching pattern, which cut pattern and only one instance of the lowest priority and will not cover any other url-pattern, but replaces the built-in default servlet servlet container, the same pattern will match All request.
- After configuring "/", a possible phenomenon is myServlet intercepts such as http: // localhost: 8080 / appDemo / 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, which is the servlet container should have a built-in "* .jsp" match , while extension matches a higher priority than the default match, why we have this phenomenon.
In Tomcat% CATALINA_HOME% \ conf \ web.xml default configuration file Servlet, 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>
- You can read http://stackoverflow.com/questions/4140448/difference-between-and-in-servlet-mapping-url-pattern
- "/ *" And "/" will intercept load static resources, need special attention
to sum up:
url-pattern configuration mode:
(1) / xxx: exact match / xxx path
(2) / xxx / *: matches the path / xxx beginning, the request must include xxx.
(3) / *: All matching path / under, or to the action request may enter Controller, but is intercepted again forwarding jsp, jsp interface can not be accessed.
(4) .xx: xx match to the end of the path, all requests must end .xx, but will not affect access to static files.
(5) /: default mode, the path has not been matched are mapped to stab servlet, for jpg, js, css and other static files will also be blocked and can not be accessed.
https://blog.csdn.net/mafan121/article/details/45035629
https://blog.csdn.net/weixin_45493751/article/details/100559683