注意,在Spring Boot中不再直接支持Servlet组件,如若使用,需要一些额外的操作!!
  扩展:监听器有如下接口

javax.servlet.ServletContextListener
javax.servlet.HttpSessionListener
javax.servlet.HttpSessionActivationListener
javax.servlet.ServletRequestListener
javax.servlet.ServletContextAttributeListener
javax.servlet.HttpSessionAttributeListener
javax.servlet.HttpSessionBindingListener
javax.servlet.ServletRequestAttributeListener

一、注解方式注册Servlet组件

(一)注册Servlet

1、自定义Servlet类:MyServlet

/**
* @WebServlet注解:指定该类为Servlet
* name:类名
* urlPatterns:映射路径(访问路径)
* initParams:Servlet初始化参数,可以在生命周期方法init()中读取该参数
*/
@WebServlet(name = "myServlet",
urlPatterns = "/myservlet",
initParams = {@WebInitParam(name = "initName", value = "initValue")}
)
public class MyServlet extends HttpServlet {

private String value;

@Override
public void init(ServletConfig config) throws ServletException {
//取出@WebServlet注解中的初始化参数
value = config.getInitParameter("initName");
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Writer writer = resp.getWriter();
writer.write("这里是servlet!");
}
}

2、在启动类上加​​@ServletComponentScan​​注解

/**
* @ServletComponentScan:扫描Servlet组件
* basePackages:指定Servlet类所在包名
*/
@SpringBootApplication
@ServletComponentScan(basePackages = {"org.pc.servlet.servlet"})
public class ServletApplication {

public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}

(二)注册Filter
  和注册Servlet差不多,不过注册注解换成​​​@WebFilter​​ 1、创建Filter

/**
* OncePerRequestFilter:该类的作用是保证每次请求,该过滤器只会被调用一次(PS:在不同的请求方式下,
* 比如forward/redirect等,过滤器的调用可能会有多次)
*
* @WebFilter:
* servletNames:指定过滤哪一个servlet
* urlPatterns:指定过滤哪一个访问请求
*/
//@WebFilter(servletNames = "myServlet")
@WebFilter(urlPatterns = "/myservlet")
public class MyFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
FilterChain filterChain) throws ServletException, IOException {
ServletContext servletContext = httpServletRequest.getServletContext();
System.out.println("调用Filter1");
filterChain.doFilter(httpServletRequest, httpServletResponse);

}
}

2、指定扫描包名

/**
* @ServletComponentScan:扫描Servlet组件
* basePackages:指定Servlet组件所在包名
*/
@SpringBootApplication
@ServletComponentScan(basePackages = {"org.pc.servlet.servlet", "org.pc.servlet.filter"})
public class ServletApplication {

public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}

(三)注册Listener
  和注册Servlet差不多,不过注册注解换成​​​@WebListener​​ 1、自定义Listener

@WebListener
public class MyListener implements ServletRequestListener {

@Override
public void requestInitialized(ServletRequestEvent sre) {
ServletRequest request = sre.getServletRequest();
ServletContext servletContext = request.getServletContext();
servletContext.log("Listener initial");
}

@Override
public void requestDestroyed(ServletRequestEvent sre) {
ServletRequest request = sre.getServletRequest();
ServletContext servletContext = request.getServletContext();
servletContext.log("Listener destroy");
}
}

2、指定扫描包名

/**
* @ServletComponentScan:扫描Servlet组件
* basePackages:指定Servlet类所在包名
*/
@SpringBootApplication
@ServletComponentScan(basePackages = {"org.pc.servlet.servlet", "org.pc.servlet.filter", "org.pc.servlet.listener"})
public class ServletApplication {

public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}

二、Spring Boot API方式注册Servlet组件
  自定义Servlet、Filter、Listener参见前面,这里主要讲如何注册

(一)注册Servlet

/**
* Spring Boot API方式注册Servlet组件
*/
@Bean
public static ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();

servletRegistrationBean.setName("myServlet2");
servletRegistrationBean.setServlet(new MyServlet2());
servletRegistrationBean.addUrlMappings("/myservlet2");
servletRegistrationBean.addInitParameter("initName", "initValue");

return servletRegistrationBean;
}

(二)注册Filter

/**
* Spring Boot API方式注册Filter组件
*/
@Bean
public static FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

filterRegistrationBean.setFilter(new MyFilter2());
filterRegistrationBean.setServletNames(Arrays.asList("myServlet2"));
/**
* 设置拦截的请求类型
*/
filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);

return filterRegistrationBean;
}

(二)注册Listener

/**
* Spring Boot API方式注册Listener组件
*/
@Bean
public static ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new MyListener2());
return servletListenerRegistrationBean;
}