servlet3.0查看官网: 属于jsr规范
查看tomcat支持servlet的版本 : 发现只有tomcat7以上才能支持servlet3.0
servlet示例:
支持WebServlet,Webfilter,WebListener注解,示例如下:
package com.atguigu.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/hello") public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doGet(req, resp); System.out.println(Thread.currentThread()+" start..."); try { sayHello(); } catch (Exception e) { e.printStackTrace(); } resp.getWriter().write("hello..."); System.out.println(Thread.currentThread()+" end..."); } public void sayHello() throws Exception{ System.out.println(Thread.currentThread()+" processing..."); Thread.sleep(3000); } }
1、Servlet容器启动会扫描,当前应用里面每一个jar包的ServletContainerInitializer接口的实现类.
2、提供ServletContainerInitializer的实现类方法:
必须绑定在,META-INF/services/javax.servlet.ServletContainerInitializer文件的内容就是ServletContainerInitializer实现类的全类名;
总结:容器在启动应用的时候,会扫描当前应用每一个jar包里面META-INF/services/javax.servlet.ServletContainerInitializer指定的实现类,启动并运行这个实现类的方法;传入感兴趣的类型;
ServletContainerInitializer;
@HandlesTypes;
原文:https://www.cnblogs.com/zhulibin2012/p/11111485.html