2019独角兽企业重金招聘Python工程师标准>>>
一、添加自定义Servlet在Spring Boot中,当需要以注解的方式添加session监听器、过滤器时,为了让其生效,还需要在配置类上添加
@ServletComponentScan
注解,该注解的作用是告诉spring boot,让其自动管理servlet,否则注解方式注册的servlet不起作用
/*** 注册Servlet. 不需要添加注解:@ServletComponentScan* @return*/@Beanpublic ServletRegistrationBean demoServlet(){ServletRegistrationBean registrationBean = new ServletRegistrationBean();registrationBean.setServlet(new DemoServlet());registrationBean.addUrlMappings("/demoServlet");return registrationBean;}
package cn.wuweixiang.springbootdemo.servlet;import javax.servlet.http.HttpServlet;
二、添加监听器
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;/*** DemoServlet** @author weixiang.wu* @date 2018-08-22 20:34**/
public class DemoServlet extends HttpServlet {&#64;Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) {System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");}&#64;Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");resp.setContentType("text/html");PrintWriter out &#61; resp.getWriter();out.println("");out.println("");out.println("");out.println("");out.println("");out.println("这是&#xff1a;DemoServlet
");out.println("");out.println("");}
}
/*** 1.使用ServletListenerRegistrationBean注册Listener&#xff0c;只需要在&#64;Configuration类中加入即可* 2.直接使用&#64;WebFilter* &#64;return*/
// &#64;Bean
// public ServletListenerRegistrationBean
// ServletListenerRegistrationBean
// demoListener.setListener(new DemoListener());
// return demoListener;
// }
package cn.wuweixiang.springbootdemo.listener;import javax.servlet.ServletContextEvent;
三、添加过滤器
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;/*** DemoListener** &#64;author weixiang.wu* &#64;date 2018-08-22 20:49**/
&#64;WebListener
public class DemoListener implements ServletContextListener {&#64;Overridepublic void contextInitialized(ServletContextEvent sce) {System.err.println("DemoListener.contextInitialized");}&#64;Overridepublic void contextDestroyed(ServletContextEvent sce) {System.err.println("DemoListener.contextDestroyed");}
}
/*** 1.使用FilterRegistrationBean注册Filter&#xff0c;只需要在&#64;Configuration类中加入即可* 2.直接使用&#64;WebFilter* &#64;see DemoFilter* &#64;return*/
// &#64;Bean
// public FilterRegistrationBean demoFilter() {
// FilterRegistrationBean demoFilter &#61; new FilterRegistrationBean();
// demoFilter.addUrlPatterns("/*");
// demoFilter.setFilter(new DemoFilter());
// return demoFilter;
// }
package cn.wuweixiang.springbootdemo.filter;import org.springframework.core.annotation.Order;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;/*** DemoFilter** &#64;author weixiang.wu* &#64;date 2018-08-22 20:43**/
&#64;WebFilter(urlPatterns &#61; "/*")
/** Lower values have higher priority.*/
&#64;Order(value &#61; 1)
public class DemoFilter implements Filter {&#64;Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.err.println("DemoFilter.init");}&#64;Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.err.println("DemoFilter.doFilter");}&#64;Overridepublic void destroy() {System.err.println("DemoFilter.destroy");}
}