Spring Boot 内嵌servlet容器的Servlets +Filters + listeners
Posted 否定之否定
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 内嵌servlet容器的Servlets +Filters + listeners相关的知识,希望对你有一定的参考价值。
一:servlet
(1)启动类
@SpringBootApplication @ServletComponentScan public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args); } }
启动类用@ServletComponentScan注解标注
(2)继承HttpServlet
@WebServlet(name = "webServlet", urlPatterns = "/webServlet", loadOnStartup = 2) public class WebServletTest extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("########doGet###########"); System.out.println("WebServletTest.doGet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("########doPost###########"); System.out.println("WebServletTest.doPost"); } }
(3)访问http://localhost:8080/webServlet 运行结果:
二:Filters
测试示例1
@WebFilter(urlPatterns = "/*",filterName="CharacterEncodeFilter", initParams={ @WebInitParam(name="encoding",value="UTF-8"), @WebInitParam(name = "forceEncoding", value = "true") }) @Singleton public class CharacterEncodingFilter implements Filter { private String encoding = "UTF-8"; private boolean forceEncoding = true; @Override public void init(FilterConfig filterConfig) throws ServletException { this.encoding = filterConfig.getInitParameter("encoding"); String force = filterConfig.getInitParameter("forceEncoding"); this.forceEncoding = (force == null) || Boolean.valueOf(force); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (this.forceEncoding || servletRequest.getCharacterEncoding() == null) { servletRequest.setCharacterEncoding(this.encoding); servletResponse.setCharacterEncoding(this.encoding); } filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
测试示例2
@WebFilter(urlPatterns = "/*", filterName = "indexFilter") public class IndexFilter implements Filter { private final static Logger logger = LoggerFactory.getLogger(IndexFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { logger.info("init IndexFilter"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { logger.info("IndexFilter.doFilter exec"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
三: listeners
示例
@WebListener public class MyServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("ServletContex初始化"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("ServletContex销毁"); } }
运行结果:
以上是关于Spring Boot 内嵌servlet容器的Servlets +Filters + listeners的主要内容,如果未能解决你的问题,请参考以下文章