zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SpringBoot整合Servlet的两种方式

SpringBootServlet 方式 整合 两种
2023-09-11 14:22:20 时间

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 }

 

有关的解释都在代码的注解里面,但是有个问题是,虽然最后可以在控制台输出相关的语句,但是浏览器的页面显示错误,应该是缺少了点什么?