一.Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 Filter、 Listener等等
二.在spring boot中的三种实现方式
方法一:通过注册ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制
servlet类:
package com.demo.example.util.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpServletRequest;public class CustomServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("servlet get method"); doPost(request , response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("servlet get method"); response.getWriter().write("Hello world"); } } filter类:
package com.demo.example.util.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;public class CustonFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub System.out.println("do destroy"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("do filter"); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("do init"); }}
Listener类:
package com.demo.example.util.listener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class CustomListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { // TODO Auto-generated method stub System.out.println("servletContextEvent"); } @Override public void contextInitialized(ServletContextEvent servletContextEvent) { // TODO Auto-generated method stub System.out.println("servletContextEvent"); }}
注册bean(第一种方式)
代码注册:(在SpringBootDemoFilerServletLisApplication类中)
@Bean public ServletRegistrationBeanservletRegistrationBean() { return new ServletRegistrationBean (new CustomServlet(), "/customServlet"); } @Bean public FilterRegistrationBean filterRegistrationBean(){ return new FilterRegistrationBean (new CustonFilter(),servletRegistrationBean() ); } @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ return new ServletListenerRegistrationBean (new CustomListener()); }
第二种代码注册:(类似一)
@SpringBootApplicationpublic class SpringBootDemoFilerServletLisApplication implements ServletContainerInitializer{ @Override public void onStartup(Set> arg0, ServletContext servletContext) throws ServletException { // TODO Auto-generated method stub servletContext.addServlet("CustomServlet", new CustomServlet()).addMapping("/customServlet"); servletContext.addFilter("CustonFilter", new CustonFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "CustomServlet"); servletContext.addListener(new CustomListener()); }
第三种基于注释:
首先:在 SpringBootDemoFilerServletLisApplication 类中添加
在listener
在servlet
在filter ( "/*" 代表过滤全局 )