上一篇,咱們講解了spring boot(json,jsp,freemarker)配置及整合方法,不清楚的能夠點擊瞭解java
@Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new HelloServlet(),"/xiaohong"); }
經過@WebServlet自動注入web
@ServletComponentScan @SpringBootApplication public class Start {……}
@WebServlet("/xiaohong") public class HelloServlet extends HttpServlet {……}
package com.wanye.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; import java.io.PrintWriter; /** * Created by wanye on 2017/5/24. */ @WebServlet("/xiaohong") public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>do get<<"); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>do post<<"); PrintWriter out = resp.getWriter(); out.print("hello @xiaohong"); out.close(); } }
過濾器Filter和監聽器Listener的實現方式與Servlet實現方式同樣,這裏列出來區別,你們本身能夠嘗試一下。spring
經過@Bean手動注入json
經過註解自動注入segmentfault
上面講解了Servlet、過濾器、監聽器,細心的同窗能夠發現,他們都是屬於javax.servlet所提供的Api。jsp
簡單來說是經過動態代理實現,被訪問的目標方法經過代理類(方法)來執行,這樣咱們就能夠在真正要執行的方法執行前、後作一些處理;經過攔截器這種方式,使得代碼能夠進行更高度的抽象。更多攔截器、動態代理的內容,請你們查閱資料瞭解。ide
在Spring中實現Http攔截器post
package com.wanye.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by wanye on 2017/5/24. */ public class HelloInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(">>interceptor preHandle<<"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(">>interceptor postHandle請求處理以後進行調用,可是在視圖被渲染以前(Controller方法調用以後)<<"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println(">>interceptor afterCompletion<<"); } }
package com.wanye.interceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * Created by wanye on 2017/5/24. */ @Configuration public class HelloConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new HelloInterceptor()).addPathPatterns("/**"); } }
本文講解了註冊Servlet/Filter/Listener的兩種⽅方式(Servlet/Filter/Listener的概念你們自行查閱資料瞭解),及攔截器基本原理,並經過註解實現http攔截器,另外本文還有一個疑問:爲何Spring中實現的Http攔截器,沒法對咱們自定義的servlet請求進行攔截?歡迎你們留言討論。學習
若本文對你有幫助,望點贊。爲了提升你們學習效果,錄製了同步的視頻課程,還望你們支持視頻課程url