SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器

常用web组件

  • 拦截器
  • Servlet
  • 过滤器
使用思想
  • 创建自定义类
  • 实现或者继承框架里的接口或类
  • 将自定义类注册到框架中
  • 使用自定义类
拦截器说明
  • 拦截器是SpringMVC中的一种对象,能拦截对Controller的请求
  • 拦截器框架中有系统的拦截器 , 还可以自定义拦截器 , 实现对请求的预先处理
自定义拦截器
  • 创建自定义的拦截器类实现SpringMVC框架的HandlerInterceptor接口,根据对拦截时机的需求 , 重写接口里的对应方法
package com.example.web.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//实际应用中被拦截的请求在这里执行其他逻辑判断,是否放行由判断结果来定System.out.println("LoginInterceptor拦截器的preHandle方法执行");return true;}}
  • 注册拦截器
package com.example.web.config;import com.example.web.interceptor.LoginInterceptor;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class MyAppConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {//创建一个拦截器对象LoginInterceptor loginInterceptor = new LoginInterceptor();//需要拦截的urlString[] interceptorUrl = {"/user/**"};//需要放行的urlString[] excludeUrl = {"/user/login"};registry.addInterceptor(loginInterceptor).addPathPatterns(interceptorUrl).excludePathPatterns(excludeUrl);}}
  • Controller层
package com.example.web.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class InterceptorTest {@RequestMapping("/user/account")@ResponseBodypublic String account(){return "访问账户页面";}@RequestMapping("/user/login")@ResponseBodypublic String login(){return "访问用户登录页面";}}Servlet
  • 创建Servlet类,继承HttpServlet
package com.example.web.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置响应类型resp.setContentType("text/html;charset=utf-8");//获取输出流对象PrintWriter out = resp.getWriter();//向浏览器输出数据out.println("--- springboot的servlet被调用 ---");out.flush();out.close();}}
  • 注册Servlet, 让框架能找到Servlet
package com.example.web.config;import com.example.web.servlet.MyServlet;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyWebConfig {//调用方法,注册servlet//@Bean//public ServletRegistrationBean servletRegistrationBean(){//return new ServletRegistrationBean(new MyServlet(), "/servlet.do");//}//注册servlet的方式2@Beanpublic ServletRegistrationBean servletRegistrationBean(){ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();servletRegistrationBean.setServlet(new MyServlet());servletRegistrationBean.addUrlMappings("/servlet1.do", "/servlet2.do");return servletRegistrationBean;}}过滤器说明
  • Filter是Servlet规范中的过滤器,可以处理请求,对请求的参数 , 属性进行调整 。常常在过滤器中处理字符编码
使用自定义过滤器
  • 创建自定义过滤器类
package com.example.web.filter;import javax.servlet.*;import java.io.IOException;public class MyFilter implements Filter {@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {servletResponse.setContentType("text/html;charset=utf-8");System.out.println("MyFilter的doFilter方法被调用");filterChain.doFilter(servletRequest, servletResponse);}}
  • 注册Filter过滤器对象
package com.example.web.config;import com.example.web.filter.MyFilter;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyFilterConfig {//定义方法,注册过滤器@Beanpublic FilterRegistrationBean filterRegistrationBean(){FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();filterRegistrationBean.setFilter(new MyFilter());filterRegistrationBean.addUrlPatterns("/user/*");return filterRegistrationBean;}}

推荐阅读