springBoot(5)---整合servlet、Filter、Listener

springBoot整合servlet、Filter、Listener需要在pom中引入如下包:

<!-- 支持 SpringMVC,Servlet,Filter,Listener -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

一、springBoot整合servlet

springBoot使用servlet的API有两种方法

方法1、使用@ServletComponentScan注解

package com.wzy.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "myServlet",urlPatterns = "/myServlet")
//上面这段配置声明该类为servlet程序,等同于在web.xml中配置<servlet>映射

@ServletComponentScan  //该注解的作用是让springBoot扫描@webServlet等注解

@SpringBootApplication  //声明为springBoot应用

public class MyFirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行了MyFirstServlet的doGet方法......");
    }
    //启动类
    public static void main(String[] args) {
        SpringApplication.run(MyFirstServlet.class,args);
    }
}

测试:浏览器中访问http://localhost:8080/myServlet,控制台打印结果如下:

方法2、使用@Bean注解

第1步、写一个HelloServlet类,继承HttpServlet类

package com.wzy.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行了HelloServlet的doGet方法......");
    }
}

第2步

package com.wzy.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

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

    //注册servlet
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new HelloServlet());
        //设置访问路径
        bean.addUrlMappings("/helloServlet");
        return bean;
    }
}

第3步、测试

浏览器输入:http://localhost:8080/helloServlet

控制台打印结果如下:

二、springBoot整合Filter

使用@WebFilter注解

package com.wzy.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "myFilter",urlPatterns = "/myFilter")
public class MyFilter 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("执行了前面代码");
        //放行,执行目标资源:/myFilter
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("执行了后面代码");
    }
    /**
     * 销毁
     * */
    @Override
    public void destroy() {

    }

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

 

三、springBoot整合Listener

使用@WebListener注解

package com.wzy.test;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("servletContext对象创建了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("servletContext对象被销毁了");
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值