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;}}
  • 注册拦截器
【SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器】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;}}

经验总结扩展阅读