springboot整合servlet、filter、interceptor
Servlet
方式一:xml注入
package com.lee.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name="firstServlet",urlPatterns = "/firstServlet")
public class FirstServlet 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("firstServlet............");
}
}
package com.lee;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class SpringbootApplicationServlet1 {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplicationServlet1.class,args);
}
}
方式二:bean注入
package com.lee.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SecondServlet 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("secondServlet....");
}
}
package com.lee;
import com.lee.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringbootApplicationServlet2 {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplicationServlet2.class,args);
}
@Bean
public ServletRegistrationBean secondServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new SecondServlet());
bean.addUrlMappings("/secondServlet");
return bean;
}
}
Filter
方式一:xml注入
package com.lee.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "firstFilter",urlPatterns = "/firstServlet")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println(" first filter init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("enter first filter");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("leave first filter");
}
@Override
public void destroy() {
System.out.println(" first filter destroy");
}
}
package com.lee;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class SpringbootApplicationFilter1 {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplicationFilter1.class,args);
}
}
方式二:bean注入
package com.lee.filter;
import javax.servlet.*;
import java.io.IOException;
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println(" second filter init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("enter second filter");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("leave second filter");
}
@Override
public void destroy() {
System.out.println(" second filter destroy");
}
}
package com.lee;
import com.lee.filter.SecondFilter;
import com.lee.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;
@SpringBootApplication
public class SpringbootApplicationFilter2 {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplicationFilter2.class,args);
}
@Bean
public ServletRegistrationBean secondServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new SecondServlet());
bean.addUrlMappings("/secondServlet");
return bean;
}
@Bean
public FilterRegistrationBean secondFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new SecondFilter());
bean.addUrlPatterns("/secondServlet");
return bean;
}
}
interceptor
public class MyInterceptor implements HandlerInterceptor {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getCanonicalName());
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
logger.info("{}:在请求处理之前进行调用(Controller方法调用之前)", this.getClass().getSimpleName());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
logger.info("{}:请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)", this.getClass().getSimpleName());
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
logger.info("{}:在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)", this.getClass().getSimpleName());
}
}
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
}
}