热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot之Servlet、Listener、Filter

2019独角兽企业重金招聘Python工程师标准在SpringBoot中,当需要以注解的方式添加session监听器、过滤器时,为了让其生效&#x

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

 在Spring Boot中,当需要以注解的方式添加session监听器、过滤器时,为了让其生效,还需要在配置类上添加@ServletComponentScan注解,该注解的作用是告诉spring boot,让其自动管理servlet,否则注解方式注册的servlet不起作用

一、添加自定义Servlet 

/*** 注册Servlet. 不需要添加注解:@ServletComponentScan* @return*/@Beanpublic ServletRegistrationBean demoServlet(){ServletRegistrationBean registrationBean = new ServletRegistrationBean();registrationBean.setServlet(new DemoServlet());registrationBean.addUrlMappings("/demoServlet");return registrationBean;}

package cn.wuweixiang.springbootdemo.servlet;import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;/*** DemoServlet** @author weixiang.wu* @date 2018-08-22 20:34**/
public class DemoServlet extends HttpServlet {&#64;Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) {System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");}&#64;Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");resp.setContentType("text/html");PrintWriter out &#61; resp.getWriter();out.println("");out.println("");out.println("");out.println("");out.println("");out.println("

这是&#xff1a;DemoServlet

");out.println("");out.println("");}
}
二、添加监听器 

/*** 1.使用ServletListenerRegistrationBean注册Listener&#xff0c;只需要在&#64;Configuration类中加入即可* 2.直接使用&#64;WebFilter* &#64;return*/
// &#64;Bean
// public ServletListenerRegistrationBean myServletListener() {
// ServletListenerRegistrationBean demoListener &#61; new ServletListenerRegistrationBean();
// demoListener.setListener(new DemoListener());
// return demoListener;
// }

package cn.wuweixiang.springbootdemo.listener;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;/*** DemoListener** &#64;author weixiang.wu* &#64;date 2018-08-22 20:49**/
&#64;WebListener
public class DemoListener implements ServletContextListener {&#64;Overridepublic void contextInitialized(ServletContextEvent sce) {System.err.println("DemoListener.contextInitialized");}&#64;Overridepublic void contextDestroyed(ServletContextEvent sce) {System.err.println("DemoListener.contextDestroyed");}
}
三、添加过滤器

/*** 1.使用FilterRegistrationBean注册Filter&#xff0c;只需要在&#64;Configuration类中加入即可* 2.直接使用&#64;WebFilter* &#64;see DemoFilter* &#64;return*/
// &#64;Bean
// public FilterRegistrationBean demoFilter() {
// FilterRegistrationBean demoFilter &#61; new FilterRegistrationBean();
// demoFilter.addUrlPatterns("/*");
// demoFilter.setFilter(new DemoFilter());
// return demoFilter;
// }

 

package cn.wuweixiang.springbootdemo.filter;import org.springframework.core.annotation.Order;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;/*** DemoFilter** &#64;author weixiang.wu* &#64;date 2018-08-22 20:43**/
&#64;WebFilter(urlPatterns &#61; "/*")
/** Lower values have higher priority.*/
&#64;Order(value &#61; 1)
public class DemoFilter implements Filter {&#64;Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.err.println("DemoFilter.init");}&#64;Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.err.println("DemoFilter.doFilter");}&#64;Overridepublic void destroy() {System.err.println("DemoFilter.destroy");}
}

 


转:https://my.oschina.net/wuweixiang/blog/1932417



推荐阅读
author-avatar
唯爱WE创丶
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有