首页 > 代码库 > springBoot(8):web开发-Servlets, Filters, listeners
springBoot(8):web开发-Servlets, Filters, listeners
一、说明
Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 Filter、Listener 等等
二、在spring boot中的三种实现方式
2.1、代码
CustomServlet.java:
package com.example.demo.utils.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 自定义 servlet * */ public class CustomServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet get method"); doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet post method"); response.getWriter().write("hello world"); } }
CustomFilter.java:
package com.example.demo.utils.filter; import javax.servlet.*; import java.io.IOException; /** * 自定义 filter * Created by DELL on 2017/6/17. */ public class CustomFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init filter"); } @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 destroy() { System.out.println("destroy filter"); } }
CustomListener.java:
package com.example.demo.utils.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * 自定义 listener * Created by DELL on 2017/6/17. */ public class CustomListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("contextInitialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("contextDestroyed"); } }
2.2、方式一:通过注册ServletRegistrationBean、FilterRegistrationBean和ServletListenerRegistrationBean
2.2.1、注册ServletRegistrationBean
Application中注册bean:
/**注册CustomServlet*/ @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new CustomServlet(), "/roncoo"); }
2.2.2、注册FilterRegistrationBean
Application中注册bean:
/**注册CustomFilter*/ @Bean public FilterRegistrationBean filterRegistrationBean() { return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean()); }
2.2.3、注册ServletListenerRegistrationBean
Application中注册bean:
/**注册CustomListener*/ @Bean public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() { return new ServletListenerRegistrationBean<CustomListener>(new CustomListener()); }
上面所有例子效果:
项目启动:
访问:http://localhost:8080/roncoo
2.3、方式二:通过实现 ServletContextInitializer 接口直接注册
Application实现 ServletContextInitializer 接口:
package com.example.demo; import com.example.demo.utils.filter.CustomFilter; import com.example.demo.utils.listener.CustomListener; import com.example.demo.utils.servlet.CustomServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import javax.servlet.DispatcherType; import javax.servlet.ServletContext; import javax.servlet.ServletException; import java.util.EnumSet; @SpringBootApplication public class SpringbootDemo27Application implements ServletContextInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/roncoo"); servletContext.addFilter("customFilter", new CustomFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet"); servletContext.addListener(new CustomListener()); } public static void main(String[] args) { SpringApplication.run(SpringbootDemo27Application.class, args); } }
2.4、方式三:
在SpringBootApplication上使用@ServletComponentScan注解后,直接通过@WebServlet、@WebFilter、@WebListener注解自动注册
@ServletComponentScan @SpringBootApplication public class SpringbootDemo27Application { //... } @WebServlet(urlPatterns = "/roncoo", name = "customServlet") public class CustomServlet extends HttpServlet { //... } @WebFilter(urlPatterns = "/*") public class CustomFilter implements Filter { //... } @WebListener public class CustomListener implements ServletContextListener { //... }
本文出自 “我爱大金子” 博客,请务必保留此出处http://1754966750.blog.51cto.com/7455444/1939406
springBoot(8):web开发-Servlets, Filters, listeners