1. 程式人生 > >SpringBoot整合Servlet的兩種方式

SpringBoot整合Servlet的兩種方式

dem javax boot 註解 auth get extend exce col

SpringBoot整合Servlet有兩種方式:

1.通過註解掃描完成Servlet組件的註冊;

2.通過方法完成Servlet組件的註冊;

現在簡單記錄一下兩種方式的實現

1.通過註解掃描完成Servlet組件的註冊;

ServletDemo1.class

 1 package com.example.combine.servlet.sbservlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 /** 12 * 13 * @author SpringBoot 整合Servlet方式1 創建servlet時需要在web.xml進行配置。 <servlet> 14 * <servlet-name>ServletDemo1</seevlet-name>
15 * <servlet-classs>com.example.combine.servlet.sbservlet.ServletDemo1</servlet-class> 16 * </servlet> 17 * 18 * <servlet-mapping> <servlet-name>ServletDemo1</servlet-name> 19 * <url-pattern>/first</url-pattern> </servlet-mapping>
20 * 21 * 但是在servlet3.0以後可以使用註釋的方式來配置,且在springboot中也沒有web.xml 22 */ 23 24 // 在哪個class添加了這個註釋就意味著哪個class就是servlet 25 @WebServlet(name = "ServletDemo1", urlPatterns = "/first") 26 public class ServletDemo1 extends HttpServlet { 27 28 @Override // 重寫doget方法 29 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 30 // TODO Auto-generated method stub 31 super.doGet(req, resp); 32 System.out.print("這是第一種方式"); //在控制臺中輸出 33 } 34 35 }

App1.class啟動類

 1 package com.example.combine.servlet.sbservlet;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.web.servlet.ServletComponentScan;
 6 //import org.springframework.web.bind.annotation.RestController;
 7 
 8 /**
 9  * SpringBoot整合servlet方式一 這種方式在控制臺看到了相關的輸出信息,但是在瀏覽器打開的時候是錯誤的頁面信息
10  */
11 @SpringBootApplication
12 @ServletComponentScan // 在SppringBoot啟動時掃描@WebServlet,並將該類實例化
13 // @RestController
14 public class App {
15     public static void main(String[] args) {
16         SpringApplication.run(App.class, args);
17     }
18 }

2.通過方法完成Servlet組件的註冊;

ServletDemo2.class

package com.example.combine.servlet.sbservlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author servlet整合springboot方式2
 *
 */

// 這種方式無須在servlet這個類中添加@WebServlet這個註釋聲明
public class ServletDemo2 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doGet(req, resp);
        System.out.println("這是第二種整合的方式");
    }

}

App2.class啟動類

 1 package com.example.combine.servlet.sbservlet;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.web.servlet.ServletRegistrationBean;
 6 import org.springframework.context.annotation.Bean;
 7 
 8 @SpringBootApplication
 9 public class App1 {
10 
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         SpringApplication.run(App1.class, args);
14     }
15 
16     // 添加如下的方法
17     // 實現在啟動類中註冊servlet的方法
18     @Bean // 添加@Bean的註釋
19     public ServletRegistrationBean getServlet() {
20         // 通過ServletRegistrationBean完成對servlet的註冊
21         ServletRegistrationBean bean = new ServletRegistrationBean(new ServletDemo2());
22         bean.addUrlMappings("/second"); // 該方法完成的是對urlPattern的配置
23         return bean; // 將對象返回
24     }
25 
26 }

有關的解釋都在代碼的註解裏面,但是有個問題是,雖然最後可以在控制臺輸出相關的語句,但是瀏覽器的頁面顯示錯誤,應該是缺少了點什麽?

SpringBoot整合Servlet的兩種方式