[JavaWeb] Two configurations about Servlet Web.xml file configuration or use @WebServlet annotation and urlPattern configuration rules

First of all, what we need to understand is that in Servlet2.x version, the configuration is still configured in the form of web.xml. After upgrading to Servlet3.x, it can be configured in an annotation-based way.

1. Configure through the Web.xml file

Code format:

    <!--注册Servlet-->
    <servlet>
    	<!--自定义,一般为类名-->
        <servlet-name></servlet-name>
        <!--servlet的类全名:package.类名-->
        <servlet-class></servlet-class>
    </servlet>
	<!--Servlet的请求路径-->
    <!--给Servlet提供(映射)一个可供客户端访问的URI-->
    <servlet-mapping>
    	<!--必须和servlet中的name相同-->
        <servlet-name></servlet-name>
        <!-- servlet的映射路径(访问serclet的名称) -->
        <url-pattern></url-pattern>
    </servlet-mapping>

Code display:

insert image description here

Access process:

①The path entered by the servlet through the browser address bar is matched with the tag value of url-pattern in the servlet-mapping tag.
② Use the mapping value of url-pattern to find the value of servlet-name in the servlet-mapping tag and match the value of servlet-name in the servlet tag.
③ After finding the servlet-name in the real servlet through the mapping relationship, find the full path under the src folder of the corresponding servlet class in the servlet-class tag in the servlet tag.
④Thereby calling and executing the corresponding servlet class.
insert image description here

Notice:

  • The value of servlet-name in the servlet-mapping tag must be the same as the servlet-name in the servlet tag.
  • servlet-class is a fully qualified name.
  • url-pattern is the corresponding address to be submitted by the web page.

2. Use the @WebServlet annotation to configure

The annotation is relatively simple, just write the annotation, fill in name and urlPatterns, name is the class name, urlPatterns is the corresponding address to be submitted by the webpage, but I usually only write urlPatterns.

Code display:

import javax.servlet.annotation.WebServlet;

@WebServlet("/demo01")
public class demo01 extends HttpServlet {
    
    }

Here are some @WebServlet annotation attributes:

insert image description here

Notice:

  • Do not specify metadata-complete="true" in the root element () of the Web.xml file; if the default metadata-complete="true", modify the attribute to "false".
  • Both the value and urlPatterns attributes in @WebServlet can be used to indicate the access path of the Servlet. Both value and urlPatterns are in the form of arrays, which means that we can map a Servlet to multiple access paths, but value and urlPatterns cannot be used at the same time. If value and urlPatterns are used at the same time, the servlet cannot access them.

urlPattern configuration extension

After the Servlet class is written, in order to be accessed, you need to configure its access path (urlPattern)

  • A Servlet can be configured with multiple URLPatterns
    insert image description here
package com.aDiao.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {
    
    "/demo04","/aDiao04"})
public class ServletDemo04 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("demo04 get...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req, resp);
    }
}

The two addresses "/demo04" and "/aDiao04" in the attached code can be accessed.

  • urlPattern configuration rules
  1. exact match
    insert image description here
package com.aDiao.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/demo05")
public class ServletDemo05 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("demo05 get...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req, resp);
    }
}

insert image description here

  1. directory match
    insert image description here
package com.aDiao.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class ServletDemo06 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("demo06 get...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req, resp);
    }
}

insert image description here
insert image description here
Thinking:
(1) Can the access path "/user" access the doGet method of ServletDemo06?
(2) Can the access path "/user/a/b" access the doGet method of ServletDemo06?
(3) Can the access path "/user/demo05" access the doGet method of ServletDemo06?
Answer: Yes, yes, no (the access is the doGet method in demo05), and we can draw the conclusion that /* in /user/* represents zero or more levels of access directories, and the priority of exact matching is higher than that of directory matches.

  1. extension match
    insert image description here
package com.aDiao.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("*.demo07")
public class ServletDemo07 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("demo07 get...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req, resp);
    }
}

insert image description here
insert image description here
Note:
(1) If the path configuration is not an extension, then you must add / in front of the path, otherwise an error will be reported;
(2) If the path configuration is *.do, then you cannot add / in front of *.do, Otherwise, an error will be reported.

  1. any match
    insert image description here
package com.aDiao.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/")
//@WebServlet("/*")
public class ServletDemo08 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("demo08 get...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req, resp);
    }
}

insert image description here
insert image description here
Note: The difference between / and /*:
(1) When the Servlet in our project is configured with "/", it will overwrite the DefaultServlet in tomcat, and this Servlet will be used when other url-patterns do not match;
( 2) When "/*" is configured in our project, it means matching any access path;
(3) DefaultServlet is used to process static resources. If "/" is configured, the default will be overwritten, and a static request will be triggered Resources did not go to the default but to go to the custom Servlet class, which eventually caused static resources to be inaccessible.

3. Let's compare the advantages and disadvantages of the two

  • Configure through the Web.xml file

Advantages: All configuration information is uninstalled in the Web.xml file, and the mapping path of each servlet class path can be managed centrally, which is convenient for modification and search.
Disadvantages: Not very friendly to "lazy cancer patients", the amount of code is large and cumbersome, and the readability is not strong.

  • Configuration using the @WebServlet annotation

Advantages: The amount of code is small, and it can be used directly in the Servlet class. Each class only focuses on its own business logic and does not interfere with each other.
Disadvantage: One class is configured one by one. If a large number of servlet annotations are used, it is inconvenient to search and modify when there are too many servlet class files.

Please correct me if I am wrong.
insert image description here

Guess you like

Origin blog.csdn.net/aDiaoYa_/article/details/126511023