SpringBoot整合Servlet/Filter/Listener

整合Servlet两种方式

方式一:写一个类继承HttpServlet 使用@WebServlet注解

@WebServlet(name = "FirstServlet",urlPatterns = "/first")
public class FristServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get servlet...");
        PrintWriter out= resp.getWriter();
        out.write("success");
        out.flush();
        out.close();
    }
}

测试类 @ServletComponentScan这个是关键 需要写上

@SpringBootApplication
@ServletComponentScan
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}

访问localhost:8080/first

方式二:写一个类继承HttpServlet 

public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get SecondServlet...");
        PrintWriter out= resp.getWriter();
        out.write("SecondServlet success");
        out.flush();
        out.close();
    }
}

测试,因为@SpringBootApplication 包含一个@Configuration 本质还是一个配置类,所以用@Configuration+@Bean的方式将servlet注入到容器 一样可以使用。

@SpringBootApplication
@ServletComponentScan
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
    //servlert的第二种实现方式(@ServletComponentScan很关键)
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return  bean;
    }
}

整合Filter两种方式

方式一:使用@WebFilter注解

@WebFilter(urlPatterns="/first")
public class FirstFilter implements Filter {
    //拦截器的第一种实现方式
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("-----FirstFilter init----------");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("-----前----------");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("-----后----------");
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

测试

@SpringBootApplication
@ServletComponentScan
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}

方式二:因为@SpringBootApplication 包含一个@Configuration 本质还是一个配置类,所以用@Configuration+@Bean的方式将fileter注入到容器 一样可以使用。

public class SecondFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("-----SecondFilter init----------");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("-----SecondFilter 前----------");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("-----SecondFilter 后----------");
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

测试类

@SpringBootApplication
@ServletComponentScan
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
    //拦截器的第二种实现方式(@ServletComponentScan很关键)
    @Bean
    public FilterRegistrationBean getRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
        bean.addUrlPatterns("/second");
        return bean;
    }
}

整合Listener两种方式

方式一:使用注解@WebListener

@WebListener
public class FirstListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("--------------FirstListener  Initialized---------------");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("--------------FirstListener  estroyed---------------");

    }
}

测试类

@SpringBootApplication
@ServletComponentScan
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}

方式二:

public class SecondListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("--------------SecondListener  Initialized---------------");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("--------------SecondListener  estroyed---------------");

    }
}

测试类

@SpringBootApplication
@ServletComponentScan
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
    //监听器的第二种实现方式(@ServletComponentScan很关键)
    @Bean
    public ServletListenerRegistrationBean getListenerRegistrationBean(){
        ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
        return  bean;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值