Servlet注册三大组件Servlet、Filter、Listener

根据Spring boot 的web开发中,我们会用到Servlet、Filter、Listener的注册,如果我们的项目中有

webapp/WEB-INF,里面会存在web.xml的配置文件,Spring Boot默认是以jar包的方式来启动嵌入式的Servlet容器,以此来启动SpringBoot的web应用,因此没有web.xml文件,但是在Spring boot没有情况下,我们也可以换一种方式进行注册

注册Servlet、Filter、Listener我们会用到对应的ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean

 

1、注册Servlet

先写一个Servlet的类继承HttpServlet 

/**
 * @description:
 * @author: 
 * @create: 2020-01-27 10:24
 **/
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello MyServlet");
    }
}

然后注册自定义的Servlet,主要实现ServletRegistrationBean 类

该类的构造方法是传入Servlet,和需要拦截的URL

 public ServletRegistrationBean(T servlet, String... urlMappings) {
        this(servlet, true, urlMappings);
    }
/**
 * @description:
 * @author: 
 * @create: 2020-01-27 10:27
 **/

@Configuration
public class MyServerConfig {

    //注册三大组件
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        return registrationBean;
    };

}

浏览器进行访问:http://localhost:8089/myServlet

则会输出:Hello MyServlet

在web开发中会涉及到一个前端控制器,在我们加入SpringMVC的时候,自动配置了前端控制器DispatcherServlet

需要注意的一点是他的拦截,默认拦截的是  /  代表拦截所有请求,包括静态资源,但是不拦截jsp请求  当我们/*这样写

才会拦截jsp请求,如果需要修改拦截的请求路径可以通过配置文件中的server.servletPath来实现

2、注册Filter

注册Filter,编写自定义的filter类实现filter所对应的接口

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("========processing=======");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

和servlet同样的注册方式

@Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return  registrationBean;
    }

3、Listener

实现ServletContextListener 

public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("web 启动");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("web 销毁");

    }
}

注册

@Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> myServletServletRegistrationBean = new ServletListenerRegistrationBean<MyListener>(new MyListener());
        return  myServletServletRegistrationBean;
    }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值