Servlet 3.0
benefit:
Support for annotation configuration, no web.xml files.
step:
(1) Create a Java EE project, Note: JavaEE version must be 6.0 or higher is supported Servlet3.0, you can not create web.xml file.
(2) the definition of a class that implements Servlet interface
(3) Method override Servlet
(4) based on the annotation @WebServlt, configure
(5) defined Java classes
1 import javax.servlet.*;
2 import javax.servlet.annotation.WebServlet;
3 import java.io.IOException;
4
5 //@WebServlet(urlPatterns = "/demo")
6 @WebServlet("/demo")
7 public class ServletDemo implements Servlet {
8 @Override
9 public void init(ServletConfig servletConfig) throws ServletException {
10
11 }
12
13 @Override
14 public ServletConfig getServletConfig() {
15 return null;
16 }
17
18 @Override
19 public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
20 System.out.println("servlet 3.0");
21 }
22
23 @Override
24 public String getServletInfo() {
25 return null;
26 }
27
28 @Override
29 public void destroy() {
30
31 }
32 }
(6) @WebServlet Code
1 //
2 // Source code recreated from a .class file by IntelliJ IDEA
3 // (powered by Fernflower decompiler)
4 //
5
6 package javax.servlet.annotation;
7
8 import java.lang.annotation.Documented;
9 import java.lang.annotation.ElementType;
10 import java.lang.annotation.Retention;
11 import java.lang.annotation.RetentionPolicy;
12 import java.lang.annotation.Target;
13
14 @Target({ElementType.TYPE})
15 @Retention(RetentionPolicy.RUNTIME)
16 @Documented
17 public @interface WebServlet {
18 String name() default ""; //相当于<Servlet-name>
19
20 String[] value() default {}; //代表urlPatterns()属性配置
21
22 String[] urlPatterns() default {}; //相当于<url-pattern>
23
24 int loadOnStartup() default -1; //相当于<load-on-startup>
25
26 WebInitParam[] initParams() default {};
27
28 boolean asyncSupported() default false;
29
30 String smallIcon() default "";
31
32 String largeIcon() default "";
33
34 String description() default "";
35
36 String displayName() default "";
37 }