一、作用:
servlet3.0支持仅支持tomcat7以上版本,@WebServlet、@WebListener、@WebFilter等注解相当于web.xml配置<servlet-name>...</<servlet-name><servlet-url>....<servlet-url>。。。。,加了此注解后web应用可以不用配置web.xml,直接注解标注相应的servlet,listener,filter
二、例子:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("hello,suxiaodong");
}
}
index.jsp
<html>
<body>
<a href="hello">servlet3.0</a>
</body>
</html>

三、ServletContainerInitializer
容器在启动应用的时候,会扫描当前应用每一个jar包里面META-INF/services/javax.servlet.ServletContainerInitializer指定的实现类,启动并运行这个实现类的方法,传入感兴趣的类型:
ServletContainerInitializer:
@HandlesTypes:
@HandlesTypes(value = {HelloService.class})
public class MyServletContainerInitializer implements ServletContainerInitializer{
//set传入注解感兴趣类型的所有子类
@Override
public void onStartup(Set<Class<?>> set, ServletContext ctx)
throws ServletException {
System.out.println("感兴趣的类型:");
for(Class<?> clazz: set) {
System.out.println(clazz);
}
}
}
interface: HelloService
实现类:HelloServiceImpl
子接口:HelloServiceSub
抽象子类:AbstractHelloService
resource配置:

容器启动打印:
2019-06-13 23:00:42.228:INFO:oejs.Server:main: jetty-9.3.12.v20160915 2019-06-13 23:00:48.602:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=5269ms 感兴趣的类型: interface com.suxiaodong.service.HelloServiceSub class com.suxiaodong.service.HelloServiceImpl class com.suxiaodong.service.AbstractHelloService 23:00:48,921 |-INFO in ch.qos.logback.classic.LoggerContext[xdsuLogging] - Could NOT find resource [logback-test.xml] 23:00:48,921 |-INFO in ch.qos.logback.classic.LoggerContext[xdsuLogging] - Could NOT find resource [logback.groovy] 23:00:48,921 |-INFO in ch.qos.logback.classic.LoggerContext[xdsuLogging] - Found resource [logback.xml] at [file:/D:/testwo