Using @WebServlet does not require web.xml configuration. When the annotation is configured at the same time as web.xml, the annotation has no effect. The advantage of using a configuration descriptor (named: web.xml) is that if you need to modify configuration values, such as the servlet path, you don't need to recompile the servlet class.
1.
The web.xml configuration is as follows:
<servlet> <servlet-name>ServletTest</servlet-name> <servlet-class>servlet.ServletTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletTest</servlet-name> <url-pattern>/servlet/myTest</url-pattern> </servlet-mapping>
The same can be achieved by annotation, as follows:
@WebServlet(name="ServletTest",urlPatterns={"/myTest"})
Since it is the information configured in the corresponding class, it is not necessary to configure the class in the annotation.
2.
Another problem, a servlet can be configured with multiple servlet-mappings in web.xml, as long as the same servlet-name is specified in it. In the annotation, you can only specify one value using value. In fact, multiple annotations can also be specified, but instead of using value, an array of urlPatterns is used. Configure as follows
@WebServlet(name="HelloWorld",urlPatterns={"/HelloWroldServlet","/HelloWorld"}). In this way, both can enter the same servlet through
http://localhost:8080/servlet/HelloWroldServlet
http://localhost:8080/servlet/HelloWorld . When the annotation is configured at the same time as web.xml, the annotation has no effect.