目录
一、SpringBoot注册web组件_注册Servlet
注册方式一:全注解方式@WebServlet
注册方式二:配置类注册Servlet
二、SpringBoot注册web组件_注册Filter
注册方式一:全注解方式@WebFilter
注册方式二:配置类注册Filter
三、SpringBoot注册web组件_注册Listener
注册方式一:全注解方式@WebListener
注册方式二:配置类注册Listener
四、知识点整理:
1.编写Firstservlet
@WebServlet("/first")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("first Servlet......");
}
}
2.启动类添加@ServletComponentScan注解扫描web组件
@SpringBootApplication
//SpringBoot启动时扫描注册注解标注的Web组件
@ServletComponentScan
public class Springbootdemo2Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo2Application.class, args);
}
}
@ServletComponentScan可以扫描Servlet、Filter、Listen三个组件
3.运行结果:后台打印出“first Servlet.....”
1.编写SecondServlet
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Second Servlet......");
}
}
2.编写配置类ServletConfig注册Servlet
@SpringBootConfiguration
public class ServletConfig {
//ServletRegistrationBean可以注册Servlet组件,将其放入Spring容器即可注册Servlet组件
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
//注册Servlet组件
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
//添加Servlet访问路径
bean.addUrlMappings("/second");
return bean;
}
}
3.运行结果:后台打印出“Second Servlet.....”
1.编写FirstFilter
@WebFilter(urlPatterns = "/first")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入first Filter");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开first Filter");
}
@Override
public void destroy() {
}
}
2.启动类中的@ServletComponentScan注解扫描web组件
@SpringBootApplication
//SpringBoot启动时扫描注册注解标注的Web组件
@ServletComponentScan
public class Springbootdemo2Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo2Application.class, args);
}
}
1.编写SecondFilter
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入Second Filter");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开Second Filter");
}
@Override
public void destroy() {
}
}
2.编写配置类FilterConfig注册Filter
@SpringBootConfiguration
public class FilterConfig {
//ServletRegistrationBean可以注册Servlet组件,将其放入Spring容器即可注册Servlet组件
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
//注册Filter组件
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
//添加Filter过滤路径
bean.addUrlPatterns("/second");
return bean;
}
}
3.运行结果:访问localhost:8080/second
1.编写FirstListener
@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("First Listener Init....");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
2.运行结果:
1.编写SecondListener
public class SecondListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Second Listener Init....");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
2.编写配置类ListenerConfig注册Listener
@Configuration
public class ListenerConfig {
@Bean
public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
return bean;
}
}
3.运行结果: