Spring-Boot 整合Servlet和Filter

<Excerpt in index | 首页摘要>

<The rest of contents | 余下全文>

整合Servlet

传统方式配置

1
2
3
4
5
6
7
8
9
10
11

<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.caicai.springbootservlet.filter.FirstFilter</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>

通过注解扫描注册Servlet

使用@WebServlet注解并在启动类上使用@ServletComponentScan注解

1
2
3
4
5
6
7
@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.....");
}
}
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@ServletComponentScan //在SpringBoot启动时会扫描@WebServlet注解 并将该类实例化
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

通过方法完成Servlet组件的注册

通过使用@Bean注解的形式注册组件

1
2
3
4
5
6
public class SecondServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("SecondServlet.....");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public ServletRegistrationBean getServletRegistrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");//添加url路径
return bean;
}
}

整合Filter

传统方式配置

1
2
3
4
5
6
7
8
9
<filter>
<filter-name>FirstFilter</filter-name>
<filter-class>com.caicai.springbootservlet.filter.FirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/filter1</url-pattern>
</filter-mapping>

通过注解扫描注册Servlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.caicai.springbootservlet.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
* @author liu
* @title: FirstFilter
* @projectName 02-spring-boot-servlet
* @description: TODO
* @date 2019/3/2718:19
*/
//@WebFilter(filterName = "",urlPatterns ={"*.do","*.jsp"})
@WebFilter(filterName = "FirstFilter",urlPatterns ="/filter1")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init.......");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入filter.......");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开filter.......");

}

@Override
public void destroy() {
System.out.println("destroy.......");

}
}

启动类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.caicai.springbootservlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan //在SpringBoot启动时会扫描@WebServlet注解 并将该类实例化
public class App3 {
public static void main(String[] args) {
SpringApplication.run(App3.class, args);
}

}

通过方法完成Filter的注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.caicai.springbootservlet.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
* @author liu
* @title: SecondFilter
* @projectName 02-spring-boot-servlet
* @description: TODO
* @date 2019/3/2718:19
*/

public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init.......");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入filter2.......");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开filter2.......");

}

@Override
public void destroy() {
System.out.println("destroy.......");

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.caicai.springbootservlet;

import com.caicai.springbootservlet.filter.SecondFilter;
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;

@SpringBootApplication
public class App4 {
public static void main(String[] args) {
SpringApplication.run(App4.class, args);
}

@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()) ;
bean.addUrlPatterns("/filter2");
return bean;
}

}