新建一个项目
新建Firstfilter类
Firstfliter.java
package com.example.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; /** *SpringBoot 整合 Filter 方式一 *<filter> * <filter-name>FirstFilter</filter-name> * <filter-class>com.bjsxt.filter.FirstFilter</filter-class> *</filter> *<filter-mapping> * <filter-name>FirstFilter</filter-name> * <url-pattern>/first</url-pattern> *</filter-mapping> */ @WebFilter(filterName = "Firstfilter",urlPatterns = "/first") public class Firstfilter implements Filter{ @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("进入 Filter"); arg2.doFilter(arg0, arg1); System.out.println("离开 Filter"); } @Override public void init(FilterConfig arg0) throws ServletException { } }
新建servlet包,同时在包内新建FirstServlet类
FirstServlet.java类
package com.example.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** *SpringBoot整合Servlet方式一 * *<servlet> * <servlet-name>FirstServlet</servlet-name> * <servlet-class>com.bjsxt.servlet.FirstServlet</servlet-class> *</servlet> * *<servlet-mapping> * <servlet-name>FirstServlet</servlet-name> * <url-pattern>/first</url-pattern> *</servlet-mapping> * */ @WebServlet(name="FirstServlet",urlPatterns="/first") public class FirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("FirstServlet............"); } }
新建一个App.java启动类
App.java类
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; /** *SpringBoot 整合 Filter 方式一 * */ @SpringBootApplication @ServletComponentScan public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } }
运行启动类
在浏览器运行
在终端打印出
现在用第二种方式整合filter
新建SecndFilter类
新建SecndServlet类
SecondServlet.java
package com.example.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** *SpringBoot整合Servlet方式二 * */ public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("SecondServlet.........."); } }
新建启动类App2.java
App2.java类
package com.example; import com.example.filter.SecondFilter; import com.example.servlet.SecondServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; /** * SpringBoot 整合 Filter 方式二 * * * */ @SpringBootApplication public class App2 { public static void main(String[] args){ SpringApplication.run(App2.class,args); } /** * 注册 Servlet * @return */ @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); return bean; } /** * 注册 Filter */
@Bean
public FilterRegistrationBean getFilterRegistrationBean() { FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); //bean.addUrlPatterns(new String[]{"*.do","*.jsp"}); bean.addUrlPatterns("/second"); return bean; } }
运行App2.java类
在浏览器输入
可以看到终端打印出的结果
原文:https://www.cnblogs.com/braveym/p/11298820.html