Servlet3.0 new feature @WebServlet

http://my.oschina.net/u/2416019/blog/633804
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.



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327043635&siteId=291194637