一.需求
Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 FilterListene
二、案例
2.1 通过注册 ServletRegistrationBean、 FilterRegistrationBean 和ServletListenerRegistrationBean 获得
package com.shyroke; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import util.MyFilter; import util.MyListener; import util.MyServlet; @SpringBootApplication public class Springboot01Application { @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new MyServlet(), "/servlet"); } @Bean public FilterRegistrationBean filterRegistrationBean() { return new FilterRegistrationBean(new MyFilter(), servletRegistrationBean()); } @Bean public ServletListenerRegistrationBean<MyListener> listenerRegistrationBean(){ return new ServletListenerRegistrationBean<MyListener>(new MyListener()); } public static void main(String[] args) { SpringApplication.run(Springboot01Application.class, args); } }
2.2 MyServlet.java
package util; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("我是servlet"); resp.getWriter().write("hello world servlet"); } }
2.3 MyFilter.java
package util; 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; public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("我是filter"); chain.doFilter(request, response); } @Override public void destroy() { // TODO Auto-generated method stub } }
2.4 MyListener.java
package util; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("contextInitialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("contextDestroyed"); } }
2.5 结果
说明servlet生效
filter和listener生效