In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
I believe many inexperienced people don't know what to do about how to write the code for springboot filters, interceptors and listeners. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Simple springboot filter, interceptor, listener use
Pom.xml
4.0.0
Sp
Spboot
0.0.1-SNAPSHOT
Org.springframework.boot
Spring-boot-starter-parent
2.0.4.RELEASE
Org.springframework.boot
Spring-boot-starter-web
Org.springframework.boot
Spring-boot-maven-plugin
Filter, servlet3 specification used
Package com.soft.m
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 org.slf4j.Logger
Import org.slf4j.LoggerFactory
Import org.springframework.stereotype.Component
@ WebFilter (filterName= "MyFilter", urlPatterns= "/ *")
Public class MyFilter implements Filter {
Private static Logger LOG = LoggerFactory.getLogger (MyFilter.class)
Public void init (FilterConfig filterConfig) throws ServletException {
LOG.info ("initialize filter")
}
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain)
Throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request
String requestURI = req.getRequestURI ()
LOG.info ("filtered request->" + requestURI)
Chain.doFilter (request, response)
}
Public void destroy () {
LOG.info ("destroy filter")
}
}
Listeners are also servlet specifications
ServletContextListener snooping
Package com.soft.m
Import javax.servlet.ServletContextEvent
Import javax.servlet.ServletContextListener
Import javax.servlet.annotation.WebListener
Import org.slf4j.Logger
Import org.slf4j.LoggerFactory
Import org.springframework.boot.web.servlet.ServletComponentScan
Import org.springframework.stereotype.Component
@ Component
@ WebListener ("WebListener")
@ ServletComponentScan ("com.soft.m")
Public class MyListener implements ServletContextListener {
Private static Logger LOG = LoggerFactory.getLogger (MyListener.class)
Public void contextInitialized (ServletContextEvent sce) {
LOG.info ("init:" + sce.getSource (). GetClass (). ToString ())
}
Public void contextDestroyed (ServletContextEvent sce) {
/ / TODO Auto-generated method stub
LOG.info ("contextDestroyed")
}
}
Session listener
Package com.soft.m
Import javax.servlet.annotation.WebListener
Import javax.servlet.http.HttpSessionEvent
Import javax.servlet.http.HttpSessionListener
Import org.slf4j.Logger
Import org.slf4j.LoggerFactory
@ WebListener
Public class MySessionListener implements HttpSessionListener {
Private static Logger LOG = LoggerFactory.getLogger (MySessionListener.class)
Public void sessionCreated (HttpSessionEvent se) {
LOG.info ("sessionCreated:" + se.getSession () .getId ())
}
Public void sessionDestroyed (HttpSessionEvent se) {
LOG.info ("sessionDestroyed:" + se.getSession () .getId ())
}
}
Servlet interceptor
Package com.soft.m
Import javax.servlet.http.HttpServletRequest
Import javax.servlet.http.HttpServletResponse
Import org.slf4j.Logger
Import org.slf4j.LoggerFactory
Import org.springframework.web.method.HandlerMethod
Import org.springframework.web.servlet.HandlerInterceptor
Import org.springframework.web.servlet.ModelAndView
Public class MyInterceptor implements HandlerInterceptor {
Private static Logger logger = LoggerFactory.getLogger (MyFilter.class)
Public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler)
Throws Exception {
HandlerMethod m = (HandlerMethod) handler
Logger.info ("preHandle:" + m.getBeanType () .toString ())
Return true
}
Public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler
ModelAndView modelAndView) throws Exception {
/ / TODO Auto-generated method stub
Logger.info ("postHandle")
}
Public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
Throws Exception {
Logger.info ("afterCompletion")
}
}
Intercept configuration
Package com.soft.m
Import org.springframework.context.annotation.Configuration
Import org.springframework.web.servlet.config.annotation.InterceptorRegistry
Import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
Import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@ Configuration
Public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
Public void addInterceptors (InterceptorRegistry registry) {
/ / TODO Auto-generated method stub
Registry.addInterceptor (new MyInterceptor ()) .addPathPatterns ("/ *")
}
Public void addResourceHandlers (ResourceHandlerRegistry registry) {
/ / registry.addResourceHandler ("/ * .ico")
}
}
Classes started by springboot
Package com.soft.m
Import org.springframework.boot.SpringApplication
Import org.springframework.boot.autoconfigure.SpringBootApplication
@ SpringBootApplication
Public class MApplication {
Public static void main (String [] args) {
SpringApplication.run (MApplication.class, args)
}
}
Controler class example
Package com.soft.m
Import javax.servlet.http.HttpServletRequest
Import org.springframework.stereotype.Controller
Import org.springframework.web.bind.annotation.RequestMapping
Import org.springframework.web.bind.annotation.ResponseBody
@ Controller
@ ResponseBody
Public class HelloController {
@ RequestMapping ("/ hello")
Public String hello () {
Return "hello method in HelloController"
}
@ RequestMapping ("/ create")
Public String create (HttpServletRequest request) {
Request.getSession () .setAttribute ("aaa")
Return "create method in HelloController"
}
@ RequestMapping ("/ destory")
Public String destory (HttpServletRequest request) {
Request.getSession () .invalidate ()
Return "destory method in HelloController"
}
}
After reading the above, have you mastered how to write the code for springboot filters and interceptors and listeners? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.