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;
    }

 

發佈了19 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章