SpringBoot整合Servlet的两种方式

时间:2022-10-07 05:50:50

SpringBoot整合Servlet有两种方式:

1.通过注解扫描完成Servlet组件的注册;

2.通过方法完成Servlet组件的注册;

现在简单记录一下两种方式的实现

1.通过注解扫描完成Servlet组件的注册;

ServletDemo1.class

 package com.example.combine.servlet.sbservlet;

 import java.io.IOException;

 import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*
* @author SpringBoot 整合Servlet方式1 创建servlet时需要在web.xml进行配置。 <servlet>
* <servlet-name>ServletDemo1</seevlet-name>
* <servlet-classs>com.example.combine.servlet.sbservlet.ServletDemo1</servlet-class>
* </servlet>
*
* <servlet-mapping> <servlet-name>ServletDemo1</servlet-name>
* <url-pattern>/first</url-pattern> </servlet-mapping>
*
* 但是在servlet3.0以后可以使用注释的方式来配置,且在springboot中也没有web.xml
*/ // 在哪个class添加了这个注释就意味着哪个class就是servlet
@WebServlet(name = "ServletDemo1", urlPatterns = "/first")
public class ServletDemo1 extends HttpServlet { @Override // 重写doget方法
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
System.out.print("这是第一种方式"); //在控制台中输出
} }

App1.class启动类

 package com.example.combine.servlet.sbservlet;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
//import org.springframework.web.bind.annotation.RestController; /**
* SpringBoot整合servlet方式一 这种方式在控制台看到了相关的输出信息,但是在浏览器打开的时候是错误的页面信息
*/
@SpringBootApplication
@ServletComponentScan // 在SppringBoot启动时扫描@WebServlet,并将该类实例化
// @RestController
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

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启动类

 package com.example.combine.servlet.sbservlet;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; @SpringBootApplication
public class App1 { public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App1.class, args);
} // 添加如下的方法
// 实现在启动类中注册servlet的方法
@Bean // 添加@Bean的注释
public ServletRegistrationBean getServlet() {
// 通过ServletRegistrationBean完成对servlet的注册
ServletRegistrationBean bean = new ServletRegistrationBean(new ServletDemo2());
bean.addUrlMappings("/second"); // 该方法完成的是对urlPattern的配置
return bean; // 将对象返回
} }

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