Use of Filter:
1. Common default filters are:
characterEncodingFilter
hiddenHttpMethodFilter
httpPutFormContentFilter
requestContextFilter
2. Priority of filter:
Ordered.highest'precision: lowest priority, value is int maximum
Ordered.low'precision: highest priority, value is int minimum
Low value means higher priority
Therefore, if you want to customize the Filter, you should avoid the same priority as the default Filter, otherwise it will conflict
3. User defined Filter
You can use Spring's FilterRegistrationBean, but this method is cumbersome
So here we use servlet 3.0:
A small Demo combined with practical application
Controller:
package org.dreamtech.springboot.controller; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { /** * View current account balance * * @return */ @RequestMapping("/login/account") private Object account() { Map<String, Object> modelMap = new HashMap<String, Object>(); modelMap.put("money", 1000); return modelMap; } }
Filter:
package org.dreamtech.springboot.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebFilter(urlPatterns = "/login/*", filterName = "loginFilter") public class LoginFilter implements Filter { /** * Called when the container is loaded */ @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("[ LoginFilter Init ]"); } /** * Called when the request is blocked */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("[ LoginFilter doFilter ]"); HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; String username = req.getParameter("username"); if (username != null) { chain.doFilter(req, resp); } else { System.out.println("[ username is null ]"); return; } } /** * Called when it is easy to be destroyed */ @Override public void destroy() { System.out.println("[ LoginFilter Destroy ]"); } }
Special note: add @ ServletComponentScan annotation to SpringBoot startup class
package org.dreamtech.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
Results after startup:
Visit: http://localhost:8080/login/account to display a blank page
Visit: http://localhost:8080/login/account?username=xxx to display {"money":1000}
Use of Servlet:
package org.dreamtech.springboot.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = "/servlet/demo", name = "demoServlet") public class DemoServlet extends HttpServlet { private static final long serialVersionUID = -428928425699550712L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("demo servlet"); resp.getWriter().flush(); resp.getWriter().close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
Note: the @ ServletComponentScan annotation should be added to the SpringBoot startup class in the same way as Filter
Use of Listener:
Common listeners are
ServletContextListener,HttpSessionListener,ServletRequestListener
ServletRequestListener: used for Statistics (such as the number of times an article has been viewed, etc.)
Controller:
@RequestMapping("/listener") private Object listener() { System.out.println("listener controller"); return "listener"; }
Listener:
package org.dreamtech.springboot.listener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; @WebListener("/listener") public class DemoListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("request destroyed"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("request initialized"); } }
The order of printing should be: each visit / listener will print these three lines
request initialized
listener controller
request destroyed
ServletContextListener: for resource loading
package org.dreamtech.springboot.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener("/listener") public class DemoListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("context initialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("context destroyed"); } }
Print order:
Print context initialized at the beginning
Print context destroyed at the end of the process
Print only once
As for the fact that HttpSessionListener is not used very much, it will not be introduced