當使用spring-Boot時,嵌入式Servlet容器通過掃描注解的方式注冊Servlet、Filter和Servlet規(guī)范的所有監(jiān)聽器(如HttpSessionListener監(jiān)聽器)。
站在用戶的角度思考問題,與客戶深入溝通,找到城固網(wǎng)站設(shè)計與城固網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計制作、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋城固地區(qū)。
Spring boot 的主 Servlet 為 DispatcherServlet,其默認的url-pattern為“/”。也許我們在應(yīng)用中還需要定義更多的Servlet,該如何使用SpringBoot來完成呢?
在spring boot中添加自己的Servlet有兩種方法,代碼注冊Servlet和注解自動注冊(Filter和Listener也是如此)。
一、代碼注冊通過ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 獲得控制。
也可以通過實現(xiàn) ServletContextInitializer 接口直接注冊。
二、在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通過 @WebServlet、@WebFilter、@WebListener 注解自動注冊,無需其他代碼。
1.通過代碼注冊Servlet示例代碼
1).SpringBootSimpleApplication.Java類:
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import com.example.servlet.MyServlet; @SpringBootApplication public class SpringBootSimpleApplication { /** * 使用代碼注冊Servlet(不需要@ServletComponentScan注解) */ @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默認值為首字母小寫,即myServlet } public static void main(String[] args) { SpringApplication.run(SpringBootSimpleApplication.class, args); } }
2).MyServlet.java類:
package com.example.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<"); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); out.println(""); out.println(""); out.println("Hello World "); out.println(""); out.println(""); out.println("大家好,我的名字叫Servlet
"); out.println(""); out.println(""); } }
2.使用注解注冊Servlet示例代碼
1).SpringBootSimpleApplication.java類:
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import com.example.servlet.MyServlet; @SpringBootApplication @ServletComponentScan public class SpringBootSimpleApplication { /** * 使用代碼注冊Servlet(不需要@ServletComponentScan注解) */ @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默認值為首字母小寫,即myServlet } public static void main(String[] args) { SpringApplication.run(SpringBootSimpleApplication.class, args); } }
2).MyServlet2.java類:
package com.example.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // 不指定name的情況下,name默認值為類全路徑,即com.example.servlet.MyServlet2 @WebServlet(urlPatterns="/st/myservlet2", description="Servlet的說明") public class Myservlet2 extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doGet2()<<<<<<<<<<<"); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doPost2()<<<<<<<<<<<"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); out.println(""); out.println(""); out.println("Hello World "); out.println(""); out.println(""); out.println("大家好,我的名字叫Servlet2
"); out.println(""); out.println(""); } }
使用 @WebServlet 注解,其中可以設(shè)置一些屬性。
3.訪問結(jié)果
4.DispatcherServlet默認攔截
DispatcherServlet 默認攔截“/”,MyServlet 攔截“/st/*”,MyServlet2 攔截“/st/myservlet”,那么在我們訪問 http://localhost:8080/st/myservlet2 的時候系統(tǒng)會怎么處理呢?如果訪問 http://localhost:8080/st/abc的時候又是什么結(jié)果呢?其結(jié)果是“匹配的優(yōu)先級是從精確到模糊,復(fù)合條件的Servlet并不會都執(zhí)行”。
既然系統(tǒng)DispatcherServlet 默認攔截“/”,那么我們是否能做修改呢,答案是肯定的,我們在SpringBootSampleApplication中添加代碼:
/** * 修改DispatcherServlet默認配置 */ @Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet); registration.getUrlMappings().clear(); registration.addUrlMappings("*.do"); registration.addUrlMappings("*.json"); return registration; }
可以通過注入DispatcherServlet 然后用ServletRegistrationBean包裹一層 動態(tài)的加上一些初始參數(shù)。