SpringBoot使用ServletAPI和拦截器
1、如何去使用ServletAPI:
package com.boot.servlet.api.bootservlet.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 = "MyServlet",urlPatterns = "/my")
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("执行了servlet的请求");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
1.1、在创建一个Filter的类,代码如下:
package com.boot.servlet.api.bootservlet.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "MyFilter",urlPatterns = "/*")
public class MyFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("执行了Filter");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
1.2、创建了一个Listener的类,代码如下:
package com.boot.servlet.api.bootservlet.filter;
package com.boot.servlet.api.bootservlet.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletContextListener{
public MyListener() {
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("servlet容器启动");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("servlet容器销毁");
}
}
上面的配置是使用的注解的方式来配置的。
在SpringBoot如何去使用ServletAPI呢?
第一种方式:使用注解的方式:@ServletComponentScan去启动。使用这个注解的条件必须是servlet和listener还有filter的类上必须有带有注解的才可以。
代码如下:
package com.boot.servlet.api.bootservlet.filter;
package com.boot.servlet.api.bootservlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class BootServletApplication {
public static void main(String[] args) {
SpringApplication.run(BootServletApplication.class, args);
}
}
然后启动下boot项目:输出的就是监听器监听到的。
去请求下MyServlet,这时的servlet的Api就引入成功了。
第二种方式:通过配置类的方式来配置,代码如下:
package com.boot.servlet.api.bootservlet.config;
import com.boot.servlet.api.bootservlet.filter.MyFilter;
import com.boot.servlet.api.bootservlet.listener.MyListener;
import com.boot.servlet.api.bootservlet.servlet.MyServlet;
import org.springframework.boot.SpringBootConfiguration;
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 java.util.Arrays;
@SpringBootConfiguration
public class ServletConfiguration {
@Bean
public ServletRegistrationBean<MyServlet> registrationBean(){
ServletRegistrationBean<MyServlet> bean=new ServletRegistrationBean<>(new MyServlet(),"/my");
return bean;
}
@Bean
public FilterRegistrationBean<MyFilter> filterFilterRegistrationBean(){
FilterRegistrationBean<MyFilter> bean=new FilterRegistrationBean<>();
bean.setFilter(new MyFilter());
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
@Bean
public ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean(){
ServletListenerRegistrationBean<MyListener> listener=new ServletListenerRegistrationBean<>();
listener.setListener(new MyListener());
return listener;
}
}
启动项目运行下:这时也被监听到。
再次访问下:
这时过滤器和servlet的请求也都打印了出来。
上面就是引入servletAPI的方式。
2、如何在springBoot中使用拦截器:
代码如下:
2.1、写一个拦截器去实现HandlerInterceptor:
package com.boot.servlet.api.bootservlet.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
2.2、写一个配置类来注册拦截器,代码如下:
package com.boot.servlet.api.bootservlet.config;
import com.boot.servlet.api.bootservlet.interceptor.MyInterceptor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@SpringBootConfiguration
public class MyWebMvcConfiguration extends WebMvcConfigurationSupport {
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor());
}
}
2.3、拦截器主要拦截的是controller,所以新建个controller,代码如下:
package com.boot.servlet.api.bootservlet.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class TestController {
@GetMapping("/index")
public void index(){
}
}
2.4、启动下项目并访问:这时就会拦截请求了。
总结:上面就是如何使用Servlet的API以及拦截器