• SpringBoot:拦截器、Servlet、过滤器(动力)



    目录:

    (1)拦截器的使用

    (2)使用Servlet

     (3)过滤器的使用

    字符集过滤的两种方式:

           (4)字符集过滤器的应用:使用自定义过滤器

           (5)在配置文件中设置编码方式


    (1)拦截器的使用

     

    创建拦截器的实现类:LoginInterceptor Ctrl+i 只实现preHandle()方法即可:

    1. package com.bjpowernode.web;
    2. import org.springframework.web.servlet.HandlerInterceptor;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. //自定义拦截器
    6. public class LoginInterceptor implements HandlerInterceptor {
    7. /*
    8. * 参数:
    9. * handler:被拦截的控制器对象
    10. * 返回值:boolean:
    11. * true:请求能被Controller处理
    12. * false:请求不能被处理器处理,请求被截断
    13. * */
    14. @Override
    15. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    16. System.out.println("执行了LoginInterceptor的preHandle方法");
    17. return true;
    18. }
    19. }

    用这个拦截器,需要把这个拦截器加入到容器中才能起作用,创建好了拦截器怎么把它加入到容器中呢?

    需要创建一个实现类,实现WebMvcConfigurer接口,这个接口有很多跟springmvc相关的功能,都在这个接口中实现了

     原来我们要注册拦截器,需要写springmvc.xml的配文件,现在把文件中的配置移到了这个接口的方法中:

    MyAppConfig:

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.web.LoginInterceptor;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.web.servlet.HandlerInterceptor;
    5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    7. @Configuration
    8. public class MyAppConfig implements WebMvcConfigurer {
    9. //用到拦截器需要实现这个方法,添加拦截器对象,注入到容器中
    10. @Override
    11. public void addInterceptors(InterceptorRegistry registry) {
    12. //创建拦截器对象
    13. HandlerInterceptor interceptor=new LoginInterceptor();
    14. //指定拦截的请求url地址
    15. String path []={"/user/**"};
    16. //指定不拦截的地址
    17. String excludePath []={"/user/login"};
    18. /*
    19. * addInterceptor()添加拦截器
    20. * addPathPatterns()添加拦截的地址
    21. * excludePathPatterns() 排除不拦截的地址
    22. * */
    23. registry.addInterceptor(interceptor).addPathPatterns(path).excludePathPatterns(excludePath);
    24. }
    25. }

    创建控制器:BootController:

    1. package com.bjpowernode.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.ResponseBody;
    5. @Controller
    6. public class BootController {
    7. @RequestMapping("/user/account")
    8. @ResponseBody
    9. public String userAccount(){
    10. return "访问user/accout地址";
    11. }
    12. //拦截器之歌方法请求不拦截
    13. @RequestMapping("/user/login")
    14. @ResponseBody
    15. public String userLogin(){
    16. return "访问user/login地址";
    17. }
    18. }

    主启动类Application:进行启动项目:

    1. package com.bjpowernode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Application.class, args);
    8. }
    9. }

    在地址栏输入: 访问user/account

     控制台会有输出:

     

    在地址栏访问:user/login

    但是控制台 没有输出:由于此请求没有被拦截,控制台没有输出

    注意这里的user/account请求拦截了,但是preHand方法中返回true所以会正常执行,

    排除:是指不经过拦截器,也就不会有输出,user/login的请求是不被拦截的

    (2)使用Servlet

     

    创建Servlet:MyServlet:

    1. package com.bjpowernode.web;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.IOException;
    7. import java.io.PrintWriter;
    8. //创建Servler类
    9. public class MyServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    12. doPost(req,resp);//Get方法中调用doPost这样无论是get请求,还是Post请求都能处理
    13. }
    14. @Override
    15. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    16. //使用HttpServletResponse输出数据,应答结果
    17. resp.setContentType("text/html;charset=utf-8");//设置内容引擎,指定数据格式,编码
    18. PrintWriter out=resp.getWriter();
    19. out.println("执行的是Servlet");
    20. out.flush();
    21. out.close();
    22. }
    23. }

    注册Servlet: 

    创建的Servlet要想能够被访问就得有对象,这个对象Servlet通过框架才能找到Servlet,所以进行Servlet进行注册,注册用到的类是ServletRegistrationBean,通过写一个配置类,定义一个方法返回值ServletRegistrationBean

    创建配置类WebApplicationConfig:先使用ServletRegistrationBean有参构造两个参数的

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.web.MyServlet;
    3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. @Configuration
    7. public class WebApplicationConfig {
    8. //通过这个方法就可以得到Servlet对象和访问地址的信息
    9. //定义方法,注册Servle对象
    10. @Bean //注解将这个对象放到容器中,通过这个对象可以找到里面存放的Servlet
    11. public ServletRegistrationBean servletRegistrationBean(){
    12. //public ServletRegistrationBean(T servlet, String... urlMappings)
    13. //第一个参数是:Servlet对象 第二个参数是:url地址
    14. ServletRegistrationBean bean=new ServletRegistrationBean(new MyServlet(),"/myservlet");
    15. return bean;
    16. }
    17. }

    主启动类:Application,进行启动项目:

    1. package com.bjpowernode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Application.class, args);
    8. }
    9. }

    在浏览器地址栏输入:

     使用ServletRegistrationBean没有参构造参数的

    WebApplicationConfig:注释掉有参数的

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.web.MyServlet;
    3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. @Configuration
    7. public class WebApplicationConfig {
    8. //通过这个方法就可以得到Servlet对象和访问地址的信息
    9. //定义方法,注册Servle对象
    10. @Bean //注解将这个对象放到容器中,通过这个对象可以找到里面存放的Servlet
    11. public ServletRegistrationBean servletRegistrationBean(){
    12. //public ServletRegistrationBean(T servlet, String... urlMappings)
    13. //第一个参数是:Servlet对象 第二个参数是:url地址
    14. //ServletRegistrationBean bean=new ServletRegistrationBean(new MyServlet(),"/myservlet");
    15. ServletRegistrationBean bean=new ServletRegistrationBean();
    16. bean.setServlet(new MyServlet());
    17. bean.addUrlMappings("/login","/test"); //相当于<url-pattern>
    18. return bean;
    19. }
    20. }

    Servlet: 

     在浏览器输入:http:localhost:8080/login

     输入另外另一个访问地址:http:localhost:8080/test

     (3)过滤器的使用

     

    创建过滤器:MyFilter:

    1. package com.bjlpokwernode.web;
    2. import javax.servlet.*;
    3. import java.io.IOException;
    4. //自定义过滤器
    5. public class MyFilter implements Filter {
    6. @Override
    7. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    8. System.out.println("执行了MyFilter,doFilter");
    9. //要想请求能够执行下去,需要来执行,过滤器那个链filterChain,这样请求就能够往后传递,能够处理请求了
    10. filterChain.doFilter(servletRequest,servletResponse);
    11. }
    12. }

     注册过滤器:

    创建配置类:WebApplicationConfig:

    1. package com.bjlpokwernode.config;
    2. import com.bjlpokwernode.web.MyFilter;
    3. import org.springframework.boot.web.servlet.FilterRegistrationBean;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. @Configuration
    7. public class WebApplicationConfig {
    8. //返回值,过滤器注册的对象
    9. @Bean
    10. public FilterRegistrationBean filterRegistrationBean(){
    11. FilterRegistrationBean bean=new FilterRegistrationBean();
    12. //指定过滤器
    13. bean.setFilter(new MyFilter());
    14. //过滤器地址
    15. bean.addUrlPatterns("/user/*");
    16. return bean;
    17. }
    18. }

     创建控制器:

    CustomFilterController:

    1. package com.bjlpokwernode.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.ResponseBody;
    5. @Controller
    6. public class CustomFilterController {
    7. @RequestMapping("/user/account")
    8. @ResponseBody
    9. public String userAccount(){
    10. return "user/account";
    11. }
    12. //不会进行过滤
    13. @RequestMapping("/querty")
    14. @ResponseBody
    15. public String queryAccount(){
    16. return "query";
    17. }
    18. }

    主启动类Application:启动项目:

    1. package com.bjlpokwernode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Application.class, args);
    8. }
    9. }

    在浏览器进行访问

     控制台输出了:过滤器中的内容

     

    输入:

    控制台没有输出:

     (4)字符集过滤器的应用:使用自定义过滤器

    CharacterEncodingFilter过滤器:不是自己写的,在springmvc框架中有这个过滤器类的 

    创建Servlet:MyServlet:

    1. package com.bjpowernode.web;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.IOException;
    7. import java.io.PrintWriter;
    8. public class MyServlet extends HttpServlet {
    9. @Override
    10. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    11. doPost(req,resp);
    12. }
    13. @Override
    14. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. resp.setContentType("text/html");
    16. PrintWriter out=resp.getWriter();
    17. out.println("在Servlet输出中文");
    18. out.flush();
    19. out.close();
    20. }
    21. }

    配置类:WebSystenConfig:

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.web.MyServlet;
    3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. @Configuration
    7. public class WebSystemConfig {
    8. //注册Servlet
    9. @Bean
    10. public ServletRegistrationBean servletRegistrationBean(){
    11. MyServlet myServlet=new MyServlet();
    12. ServletRegistrationBean reg=new ServletRegistrationBean(myServlet,"/myservlet");
    13. return reg;
    14. }
    15. }

    主启动类Application:启动项目:

    1. package com.bjpowernode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Application.class, args);
    8. }
    9. }

    在浏览器访问:发现输出的中文乱码 

     默认编码方式IOS-8859-1

     设置字符集过滤器

     使用字符集过滤器:

    WebSystenConfig:注册过滤器

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.web.MyServlet;
    3. import org.springframework.boot.web.servlet.FilterRegistrationBean;
    4. import org.springframework.boot.web.servlet.ServletRegistrationBean;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.web.filter.CharacterEncodingFilter;
    8. @Configuration
    9. public class WebSystemConfig {
    10. //注册Servlet
    11. @Bean
    12. public ServletRegistrationBean servletRegistrationBean(){
    13. MyServlet myServlet=new MyServlet();
    14. ServletRegistrationBean reg=new ServletRegistrationBean(myServlet,"/myservlet");
    15. return reg;
    16. }
    17. //注册过滤器:
    18. @Bean
    19. public FilterRegistrationBean filterRegistrationBean(){
    20. FilterRegistrationBean reg=new FilterRegistrationBean();
    21. //使用框架中的过滤器
    22. CharacterEncodingFilter filter=new CharacterEncodingFilter();
    23. //设置过滤器参数
    24. filter.setEncoding("utf-8");//指定编码方式
    25. filter.setForceEncoding(true);//指定request,response都是用encoding的值
    26. //添加过滤器
    27. reg.setFilter(filter);
    28. //指定过滤器过滤的url地址 /*代表所有地址
    29. reg.addUrlPatterns("/*");
    30. return reg;
    31. }
    32. }

    在配置文件application.properties中:

    1. #SpringBoot中默认已经配置了CharacterEncodingFilter,true,默认编码IOS-8859-1
    2. #需要设置enabled=false,作用是关闭系统中配置好的过滤器,使用自定义的CharacterEncodingFliter
    3. server.servlet.encoding.enabled=false

    使用过滤器之后:

     (5)在配置文件中设置编码方式

     MyServlet:

    1. package com.bjpowernode.web;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.IOException;
    7. import java.io.PrintWriter;
    8. public class MyServlet extends HttpServlet {
    9. @Override
    10. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    11. doPost(req,resp);
    12. }
    13. @Override
    14. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. resp.setContentType("text/html");
    16. PrintWriter out=resp.getWriter();
    17. out.println("******在Servlet输出中文,默认编码IOS-8859-1*********");
    18. out.flush();
    19. out.close();
    20. }
    21. }

    配置类:WebSystenConfig:

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.web.MyServlet;
    3. import org.springframework.boot.web.servlet.FilterRegistrationBean;
    4. import org.springframework.boot.web.servlet.ServletRegistrationBean;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.web.filter.CharacterEncodingFilter;
    8. @Configuration
    9. public class WebSystemConfig {
    10. //注册Servlet
    11. @Bean
    12. public ServletRegistrationBean servletRegistrationBean(){
    13. MyServlet myServlet=new MyServlet();
    14. //第一个参数的过滤器对象,第二个是请求方式请求地址
    15. ServletRegistrationBean reg=new ServletRegistrationBean(myServlet,"/myservlet");
    16. return reg;
    17. }
    18. }

    配置文件application.properties:

    1. #端口号
    2. server.port=9001
    3. #访问路径
    4. server.servlet.context-path=/myboot
    5. #让系统的CharacterEncodingFilter生效
    6. server.servlet.encoding.enabled=true
    7. #指定编码方式
    8. server.servlet.encoding.charset=utf-8
    9. #设置请求和应答对象都是用charset的值
    10. server.servlet.encoding.force=true

    主启动类:启动项目

    1. package com.bjpowernode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Application.class, args);
    8. }
    9. }

    在浏览器输入地址: 

  • 相关阅读:
    vue3 | defineExpose的使用
    block 归纳总结 上
    (っ•̀ω•́)っ 如何在PPT中为文本框添加滚动条
    android手机平板拓展电脑音频
    Kubernetes的原理及应用详解(三)
    基于BRNN的政务APP评论端到端方面级情感分析方法
    【百战GAN】SRGAN人脸低分辨率老照片修复代码实战
    leetCode 567. 字符串的排列
    canoe的创建
    基于docker环境的tomcat开启远程调试
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125537173