标签:try 方式 const nat object replace inf stat 过滤
工程结构:
主方法类:
package com.boot.servlet.api.bootservlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration; // @ServletComponentScan //扫描的方式使用servlet @SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class) public class BootServletApplication { public static void main(String[] args) { SpringApplication.run(BootServletApplication.class, args); } }
Setvlet
package com.boot.servlet.api.bootservlet.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; @WebServlet(name = "MyServlet",urlPatterns = "/my") public class MyServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("执行了servlet请求"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
过滤器
package com.boot.servlet.api.bootservlet.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(filterName = "MyFilter", urlPatterns = "/*") public class MyFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { System.out.println("执行filter"); chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { } }
监听器
package com.boot.servlet.api.bootservlet.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import javax.servlet.http.HttpSessionBindingEvent; @WebListener public class Listener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener { // Public constructor is required by servlet spec public Listener() { } // ------------------------------------------------------- // ServletContextListener implementation // ------------------------------------------------------- public void contextInitialized(ServletContextEvent sce) { /* This method is called when the servlet context is initialized(when the Web application is deployed). You can initialize servlet context related data here. */ System.out.println("servlet容器启动"); } public void contextDestroyed(ServletContextEvent sce) { /* This method is invoked when the Servlet Context (the Web application) is undeployed or Application Server shuts down. */ } // ------------------------------------------------------- // HttpSessionListener implementation // ------------------------------------------------------- public void sessionCreated(HttpSessionEvent se) { /* Session is created. */ } public void sessionDestroyed(HttpSessionEvent se) { /* Session is destroyed. */ } // ------------------------------------------------------- // HttpSessionAttributeListener implementation // ------------------------------------------------------- public void attributeAdded(HttpSessionBindingEvent sbe) { /* This method is called when an attribute is added to a session. */ } public void attributeRemoved(HttpSessionBindingEvent sbe) { /* This method is called when an attribute is removed from a session. */ } public void attributeReplaced(HttpSessionBindingEvent sbe) { /* This method is invoked when an attibute is replaced in a session. */ } }
除了使用注解外还可以使用配置类的方式实现:
package com.boot.servlet.api.bootservlet.config; import com.boot.servlet.api.bootservlet.filter.MyFilter; import com.boot.servlet.api.bootservlet.listener.Listener; import com.boot.servlet.api.bootservlet.servlet.MyServlet; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import java.util.Arrays; /** * 通过配置类的方式引入servlet */ @SpringBootConfiguration public class ServletConfiguration { @Bean public ServletRegistrationBean<MyServlet> registrationBean() { ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<>(); bean.setServlet(new MyServlet()); bean.setUrlMappings(Arrays.asList("/my")); return bean; } @Bean public FilterRegistrationBean<MyFilter> filterFilterRegistrationBean() { FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean<>(); bean.setFilter(new MyFilter()); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } @Bean public ServletListenerRegistrationBean<Listener> servletListenerRegistrationBean() { ServletListenerRegistrationBean<Listener> listener = new ServletListenerRegistrationBean<>(); listener.setListener(new Listener()); return listener; } }
拦截器
package com.boot.servlet.api.bootservlet.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInteceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion"); } }
拦截器配置类
package com.boot.servlet.api.bootservlet.config; import com.boot.servlet.api.bootservlet.interceptor.MyInteceptor; import org.springframework.boot.SpringBootConfiguration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * 拦截器配置类 */ @SpringBootConfiguration public class MyWebMvcConfiguration extends WebMvcConfigurationSupport { @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInteceptor()); } }
标签:try 方式 const nat object replace inf stat 过滤
原文地址:https://www.cnblogs.com/lm970585581/p/9822515.html