您好,登錄后才能下訂單哦!
一、說明
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 { //... }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。