当前位置:网站首页>2. 整合 Filter
2. 整合 Filter
2022-06-28 19:51:00 【Yuyy】
- 通过注解扫描完成 Filter 组件的注册
- 在 servlet3.0 之后,我们可以使用注解 @WebFilter 的方式进行配置过滤器,该方式过滤不用去 xml 文件中配置就可以使用,但是默认过滤器顺序是按照类名来控制,如果对过滤器顺序有严格的要求,则需要在 web.xml 中定义(按先后顺序)。
- 1.1 编写 Filter /***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={"*.do","*.jsp"})
@WebFilter(filterName="FirstFilter",urlPatterns="/first")
public class FirstFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@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 {
// TODO Auto-generated method stub
}
}/***SpringBoot 整合 Filter 方式一 **/
@SpringBootApplication @ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}- 通过方法完成 Filter 组件的注册
- 2.1 编写 Filter
/***SpringBoot 整合 Filter 方式二 **/
public class SecondFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
System.out.println("进入 SecondFilter");
arg2.doFilter(arg0, arg1);
System.out.println("离开 SecondFilter");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
/*** 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;
}
}Post Views: 377
边栏推荐
- 1002_ twenty million one hundred and eighty-one thousand and nineteen
- Parallax JS special effect JS carousel map plug-in
- F (x) construct the equation, calculate the partial derivative by gradient descent, determine the partial derivative adjustment by loss function, and deal with nonlinear problems by activation functio
- 2342
- risc-v指令集
- head、tail查看文件
- 阿里开源(EasyExcel)
- Intelligent computing system 1 environment construction
- Rsync remote synchronization
- C语言局部变量与全局变量重名时的优先级问题(当局部变量和全局变量同时存在的时候,优先引用局部变量,而不去引用全局变量)
猜你喜欢

Ali open source (easyexcel)

5G NR MBS架构介绍

R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions

Windows 64 bit download install my SQL
![return new int[]{i + 1, mid + 1}; return {i + 1, mid + 1};](/img/6a/45a4494276deba72ef9833818229f5.png)
return new int[]{i + 1, mid + 1}; return {i + 1, mid + 1};

Are you still paying for your thesis? Come and join me

电子科大(申恒涛团队)&京东AI(梅涛团队)提出用于视频问答的结构化双流注意网络,性能SOTA!优于基于双视频表示的方法!...

I. The HR system is put on the enterprise wechat ISV to enhance the in-depth application of enterprise wechat in service chain retail and other industries

论文阅读:Duplex Contextual Relation Network for Polyp Segmentation

《数据安全法》出台一周年,看哪四大变化来袭?
随机推荐
MDM data analysis function description
522. 最长特殊序列 II(贪心&双指针)
C language - function knowledge points
电子科大(申恒涛团队)&京东AI(梅涛团队)提出用于视频问答的结构化双流注意网络,性能SOTA!优于基于双视频表示的方法!...
Bayesian inference problem, MCMC and variational inference
Ali open source (easyexcel)
Pyinstaller打包pikepdf失败的问题排查
2022 t elevator repair test question bank simulation test platform operation
Design of secsha system
5g NR MBS architecture introduction
Grep text search tool
redisTemplate
核芯物联蓝牙aoa定位系统服务器配置估算
Pipeline | and redirection >
I. The HR system is put on the enterprise wechat ISV to enhance the in-depth application of enterprise wechat in service chain retail and other industries
Machine learning notes temperature+softmax
秒杀系统设计
C语言局部变量与全局变量重名时的优先级问题(当局部变量和全局变量同时存在的时候,优先引用局部变量,而不去引用全局变量)
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
Autumn recruitment experience sharing | how to prepare for bank interview