您好,登錄后才能下訂單哦!
當使用spring-Boot時,嵌入式Servlet容器通過掃描注解的方式注冊Servlet、Filter和Servlet規范的所有監聽器(如HttpSessionListener監聽器)。
Spring boot 的主 Servlet 為 DispatcherServlet,其默認的url-pattern為“/”。也許我們在應用中還需要定義更多的Servlet,該如何使用SpringBoot來完成呢?
在spring boot中添加自己的Servlet有兩種方法,代碼注冊Servlet和注解自動注冊(Filter和Listener也是如此)。
一、代碼注冊通過ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 獲得控制。
也可以通過實現 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("<html>"); out.println("<head>"); out.println("<title>Hello World</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>大家好,我的名字叫Servlet</h2>"); out.println("</body>"); out.println("</html>"); } }
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("<html>"); out.println("<head>"); out.println("<title>Hello World</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>大家好,我的名字叫Servlet2</h2>"); out.println("</body>"); out.println("</html>"); } }
使用 @WebServlet 注解,其中可以設置一些屬性。
3.訪問結果
4.DispatcherServlet默認攔截
DispatcherServlet 默認攔截“/”,MyServlet 攔截“/st/*”,MyServlet2 攔截“/st/myservlet”,那么在我們訪問 http://localhost:8080/st/myservlet2 的時候系統會怎么處理呢?如果訪問 http://localhost:8080/st/abc的時候又是什么結果呢?其結果是“匹配的優先級是從精確到模糊,復合條件的Servlet并不會都執行”。
既然系統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包裹一層 動態的加上一些初始參數。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。