SpringBoot之Filter And Listener(D)详解编程语言

过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet 一样;[email protected]@WebListener的方式完成

Filter

MyFilter.Java

package cn.wuyang.springboot.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; 
 
/** 
 * 使用注解标注过滤器 
 * @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器 
 * 属性filterName声明过滤器的名称,可选 
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性) 
 *  
 */ 
@WebFilter(filterName="myFilter",urlPatterns="/*") 
public class MyFilter implements Filter { 
 
    @Override 
    public void destroy() { 
        System.out.println("过滤器销毁"); 
 
    } 
 
    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
            throws IOException, ServletException { 
        System.out.println("执行过滤操作"); 
        request.setCharacterEncoding("UTF-8"); 
        response.setCharacterEncoding("UTF-8"); 
        response.setContentType("text/html;charset=utf-8"); 
        chain.doFilter(request, response); 
 
    } 
 
    @Override 
    public void init(FilterConfig arg0) throws ServletException { 
        System.out.println("过滤器销毁"); 
 
    } 
 
} 

Listener

MyHttpSessionListener.java

package cn.wuyang.springboot.listener; 
 
import javax.servlet.annotation.WebListener; 
import javax.servlet.http.HttpSessionEvent; 
import javax.servlet.http.HttpSessionListener; 
 
@WebListener 
public class MyHttpSessionListener implements HttpSessionListener { 
 
    @Override 
    public void sessionCreated(HttpSessionEvent arg0) { 
        System.out.println("Session 被创建"); 
 
    } 
 
    @Override 
    public void sessionDestroyed(HttpSessionEvent arg0) { 
         System.out.println("ServletContex初始化"); 
 
    } 
 
} 

MyServletContextListener.java

package cn.wuyang.springboot.listener; 
 
import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener; 
import javax.servlet.annotation.WebListener; 
 
@WebListener 
public class MyServletContextListener implements ServletContextListener { 
 
    @Override 
    public void contextDestroyed(ServletContextEvent arg0) { 
        System.out.println("ServletContex销毁"); 
 
    } 
 
    @Override 
    public void contextInitialized(ServletContextEvent arg0) { 
        System.out.println("ServletContex初始化"); 
        System.out.println(arg0.getServletContext().getServerInfo()); 
 
    } 
 
} 

运行启动访问页面后台 console输出

  .   ____          _            __ _ _ 
/// / ___'_ __ _ _(_)_ __  __ _ / / / / 
( ( )/___ | '_ | '_| | '_ // _` | / / / / 
///  ___)| |_)| | | | | || (_| |  ) ) ) ) 
'  |____| .__|_| |_|_| |_/__, | / / / / 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::        (v1.5.6.RELEASE) 
2017-08-16 14:42:51.708  INFO 136708 --- [           main] cn.wuyang.springboot.DemoApplication     : Starting DemoApplication on DESKTOP-PARJK8F with PID 136708 (D:/eclipse_spring_boot/sts-bundle/sts-3.9.0.RELEASE/workspace/demo/target/classes started by wuyang in D:/eclipse_spring_boot/sts-bundle/sts-3.9.0.RELEASE/workspace/demo) 
2017-08-16 14:42:51.710  INFO 136708 --- [           main] cn.wuyang.springboot.DemoApplication     : No active profile set, falling back to default profiles: default 
2017-08-16 14:42:51.745  INFO 136708 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6b0c2d26: startup date [Wed Aug 16 14:42:51 CST 2017]; root of context hierarchy 
2017-08-16 14:42:52.547  INFO 136708 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 
2017-08-16 14:42:52.563  INFO 136708 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat] 
2017-08-16 14:42:52.563  INFO 136708 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.16 
2017-08-16 14:42:52.714  INFO 136708 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
2017-08-16 14:42:52.716  INFO 136708 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 
2017-08-16 14:42:52.716  INFO 136708 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 971 ms 
2017-08-16 14:42:52.832  INFO 136708 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/] 
2017-08-16 14:42:52.832  INFO 136708 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'cn.wuyang.springboot.servlet.MyServlet' to [/myservlet] 
2017-08-16 14:42:52.832  INFO 136708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*] 
2017-08-16 14:42:52.832  INFO 136708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 
2017-08-16 14:42:52.832  INFO 136708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*] 
2017-08-16 14:42:52.832  INFO 136708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*] 
2017-08-16 14:42:52.832  INFO 136708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'myFilter' to urls: [/*] 
ServletContex初始化 
Apache Tomcat/8.5.16 
过滤器销毁 
2017-08-16 14:42:53.064  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6b0c2d26: startup date [Wed Aug 16 14:42:51 CST 2017]; root of context hierarchy 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/info]}" onto public java.util.Map<java.lang.String, java.lang.String> cn.wuyang.springboot.controller.HelloController.getInfo(java.lang.String) 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String cn.wuyang.springboot.controller.HelloController.hello() 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/list]}" onto public java.util.List<java.util.Map<java.lang.String, java.lang.String>> cn.wuyang.springboot.controller.HelloController.getList() 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/page/ || /page/index]}" onto public java.lang.String cn.wuyang.springboot.controller.PageController.index(java.util.Map<java.lang.String, java.lang.Object>) 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/page/page2]}" onto public java.lang.String cn.wuyang.springboot.controller.PageController.page2(org.springframework.ui.Model) 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/page/page1]}" onto public org.springframework.web.servlet.ModelAndView cn.wuyang.springboot.controller.PageController.page1() 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 
2017-08-16 14:42:53.095  INFO 136708 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 
2017-08-16 14:42:53.117  INFO 136708 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2017-08-16 14:42:53.117  INFO 136708 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2017-08-16 14:42:53.148  INFO 136708 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2017-08-16 14:42:53.264  INFO 136708 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 
2017-08-16 14:42:53.318  INFO 136708 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 
2017-08-16 14:42:53.333  INFO 136708 --- [           main] cn.wuyang.springboot.DemoApplication     : Started DemoApplication in 1.804 seconds (JVM running for 2.68) 
2017-08-16 14:42:56.903  INFO 136708 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet' 
2017-08-16 14:42:56.903  INFO 136708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started 
2017-08-16 14:42:56.919  INFO 136708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms 
执行过滤操作 
Hello application wuyang 
Session 被创建

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11263.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论