Web controller component of the three components of the Servlet (reprint)

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.

HttpServlet features : Depending on the request method, doXxx method for processing a user request. doGet () / doPost (), etc., can be customized ---- front controller.
             Methods Http service request and response objects are converted into HttpServletRequest and HttpServletResponse type.
A few core objects:    
  Servlet class inherits an abstract class HttpServlet interface or GenerivServlet
  ServletConfig Interface: its object encapsulates a secondary Servlet configuration information.
  ServeltContext Interface: its object encapsulates all the information of the current web application, the sharing of data between a plurality of Servlet.
   Role: Get initialization parameters of the web application.
                A plurality of servlet objects implement a shared data objects [domain].
                   Reading resource files in the web application.

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.

 Note: If not automatically create a Servlet, Servlet both need to manually write the configuration files and configuration information ---- There are two configuration methods.
------------------------------------------- The following [Reserved] - -------------------------------------------------
Annotations configuration : Servlet3.0 introduced annotation specification, by using the Servlet class ( "") @WebServlet marked annotations.
  For @WebServlet concerned, its attributes - access path must exist, and represents an access path attribute is a value and urlPatterns.
  For example: annotation @WebServlet ( "/ category"), indicates that the default request Servlet path ... / category, name attribute is omitted here urlPatterns,
  Complete wording should be: @WebServlet (urlPatterns = "/ category").
     If you need to set multiple properties in @WebServlet, you must add the attribute name to attribute values, separated by commas middle, otherwise it will error.
  If the name attribute is not set @WebServlet, the default value is the full name of the Servlet class.

  

 
In the global configuration file to configure Web.xml :
  1) servlet-name: Specifies the name of the servlet, the unique property in web.xml.
  2) servlet-class: specifies the name of servlet classes - the class name in all paths
  3) init-param: for initialization parameters specified servlet may be acquired by HttpServlet.getInitParameter in the application.
  4) load-on-startup: used to control the Web application startup, the load order Servlet. Value is less than 0, when the web application starts, it does not load the servlet, load the first access.
  5) enabled: true, false. If false, indicating that the request does not process any Servlet.
  6) url-pattern: URL used to specify an expression, a plurality of servlet-mapping may be configured url- pattern simultaneously.
 
 Mapping the servlet--------- -> URL patter-label mapping configuration

First need to clear a few confusing rules:

  1. 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 .
  2. 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;
  3. 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
  4. url-pattern matching process are mapped priority
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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

Copy the 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>
Copy the code
  • 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  

Guess you like

Origin www.cnblogs.com/Tanggula-pioneer/p/11705862.html