SpringBoot整合Servlet和Filter和Listener組件詳解
整合Servlet、Filter和Listener
一般情況 下,使用Spring、Spring MVC這些框架之后,基本上就告別Servlet、Filter以及Listener了。
但是有時(shí)在整合一些第三方框架時(shí),可能還是不得不使用Servlet, 比如在整合某報(bào)表插件時(shí)就需要使用Servlet。
Spring Boot中對(duì)于整合這些基本的Web組件也提供了很好的支持。
在一個(gè)Spring Boot Web項(xiàng)目中添加如下三個(gè)組件: (import javax.servlet.*; 下的類)
@WebServlet("/my") 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 { System.out.println("name>>>" + req.getParameter("name")); } }
@WebFilter("/*") public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("MyFilter>>>init"); } @Override public void destroy() { System.out.println("MyFilter>>>doFilter"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("MyFilter>> >destroy"); } }
@WebListener public class MyListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("MyListener>>> requestDestroyed"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("MyListener>> > requestInitialized"); } }
代碼解釋:
- 這里定義了 三個(gè)基本的組件,分別使用@WebServlet、@WebFilter 和@WebListener三個(gè)注解進(jìn)行標(biāo)記。
- 這里以 ServletRequestListener為例,但是對(duì)于其他的Listener, 例如HttpSessionI istener、ServletContextListener等也是支持的。
在項(xiàng)目入口類上添加@ServletComponentScan注解,實(shí)現(xiàn)對(duì)Servlet、Filter以及Listener的掃描,代碼如下:
@SpringBootApplication @ServletComponentScan public class TestspringbootApplication { public static void main(String[] args) { SpringApplication.run(TestspringbootApplication.class, args); } }
最后,啟動(dòng)項(xiàng)目,在瀏覽器中輸入"//localhost:8080/my?name=Michael",可以看到相關(guān)日志。
MyListener>> > requestInitialized
MyFilter>> >destroy
MyListener>>> requestDestroyed
MyListener>> > requestInitialized
MyFilter>> >destroy
MyListener>>> requestDestroyed
到此這篇關(guān)于SpringBoot整合Servlet和Filter和Listener組件詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合Web組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
j2ee之AJAX二級(jí)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了j2ee之AJAX二級(jí)聯(lián)動(dòng)效果的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08springboot實(shí)現(xiàn)單文件和多文件上傳
這篇文章主要為大家詳細(xì)介紹了springboot實(shí)現(xiàn)單文件和多文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Java設(shè)置httponly?cookie的實(shí)現(xiàn)示例
本文主要介紹了Java設(shè)置httponly?cookie的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重復(fù)讀取配置文
這篇文章主要介紹了Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重復(fù)讀取配置文件的方法,需要的朋友可以參考下2015-12-12一文學(xué)會(huì)如何在SpringBoot中使用線程池執(zhí)行定時(shí)任務(wù)
在開(kāi)發(fā)現(xiàn)代應(yīng)用程序時(shí),定時(shí)任務(wù)是一項(xiàng)常見(jiàn)的需求,SpringBoot提供了一個(gè)強(qiáng)大的定時(shí)任務(wù)框架,可以輕松地執(zhí)行各種定時(shí)任務(wù),結(jié)合線程池的使用,可以更好地管理任務(wù)的執(zhí)行,提高系統(tǒng)的性能和穩(wěn)定性,本文將介紹如何在Spring Boot中使用線程池執(zhí)行定時(shí)任務(wù)2023-06-06基數(shù)排序簡(jiǎn)介及Java語(yǔ)言實(shí)現(xiàn)
這篇文章主要介紹了基數(shù)排序簡(jiǎn)介及Java語(yǔ)言實(shí)現(xiàn),涉及基數(shù)排序的基本思想簡(jiǎn)單介紹和桶排序的分析,以及基數(shù)排序的Java實(shí)現(xiàn),具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11Springboot初始化啟動(dòng)報(bào)錯(cuò)Error?creating?bean?with?name?'da
這篇文章主要為大家介紹了Springboot初始化啟動(dòng)報(bào)Error?creating?bean?with?name?'dataSource'?defined?in?class?path?resource解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08