Spring Boot filters and listeners

Source: Internet
Author: User

Spring Boot filters and listeners

The previous article has explained the Servlet-defined methods. The registration methods for filters and Listener are the same as those for servlets. In this article, we will directly use the @ WebFilter and @ WebListener methods, complete a Filter and a Listener.

Filter files

MyFilter. java

Package org. springboot. sample. filter; import java. io. IOException; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. annotation. webFilter;/*** use annotation filter * @ WebFilter to implement javax. servlet. the Filter interface class is defined as the Filter * attribute filterName to declare the name of the Filter. Optional. The * attribute urlPatterns specifies the URL mode to Filter. You can also use the attribute value to declare the Filter. (specifying the URL mode to be filtered is a required attribute) ** @ author single redwoo (365384722) * @ myblog http://blog.csdn.net/catoop/ * @ create January 6, 2016 */@ WebFilter (filterName = "myFilter ", urlPatterns = "/*") public class MyFilter implements Filter {@ Override public void destroy () {System. out. println ("filter destruction") ;}@ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System. out. println ("filter operation"); chain. doFilter (request, response) ;}@ Override public void init (FilterConfig config) throws ServletException {System. out. println ("filter initialization ");}}

ServletContext Listener (Listener) File

MyServletContextListener. java

Package org. springboot. sample. listener; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; import javax. servlet. annotation. webListener;/*** use the @ WebListener annotation to implement the ServletContextListener interface ** @ author single Hongyu (365384722) * @ myblog http://blog.csdn.net/catoop/ * @ create January 6, 2016 */@ WebListenerpublic class MyServletContextListener implements ServletContextListener {@ Override public void contextInitialized (ServletContextEvent sce) {System. out. println ("ServletContex initialization"); System. out. println (sce. getServletContext (). getServerInfo () ;}@ Override public void contextDestroyed (ServletContextEvent sce) {System. out. println ("ServletContex destruction ");}}

ServletContext Listener (Listener) File

MyHttpSessionListener. java

Package org. springboot. sample. listener; import javax. servlet. annotation. webListener; import javax. servlet. http. httpSessionEvent; import javax. servlet. http. httpSessionListener;/*** listen to Session creation and destruction ** @ author single Hongyu (365384722) * @ myblog http://blog.csdn.net/catoop/ * @ create January 6, 2016 */@ WebListenerpublic class MyHttpSessionListener implements HttpSessionListener {@ Override public void sessionCreated (HttpSessionEvent se) {System. out. println ("Session created");} @ Override public void sessionDestroyed (HttpSessionEvent se) {System. out. println ("ServletContex initialization ");}}

Do not forget to add the @ ServletComponentScan Annotation on SpringBootSampleApplication. java.

During the startup process, we will see the output:

ServletContex initialize Apache Tomcat/8.0.30 filter Initialization

After the service is started, you can access a page and see the output:

The Session for performing the filter operation is created.

For how to use the code to register the Filter and Listener, refer to the introduction of the key Servlet in the previous article. The difference is that FilterRegistrationBean and ServletListenerRegistrationBean must be used.

The last project structure:

The Code does not currently have a warning ^_^

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.