Spring Boot整合Servlet使用

本文介绍如何在SpringBoot中整合Servlet,提供了两种方法:一是通过扫描注解整合,二是使用@Bean注解注册Servlet组件。通过具体代码示例展示了如何实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 方法一:通过扫描注解整合

  1. 编写servlet

    @WebServlet(name="MyServlet",urlPatterns="/first")
    public class MyServlet extends HttpServlet {
    	
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		System.out.println("Hello  World!!!!!!!!!!");
    	}
    }

    2.  编写启动类

@SpringBootApplication
@ServletComponentScan            //该注解写上以后会自动寻找包含@WebServlet注解的类,并实例化
public class ServletSpringApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServletSpringApplication.class, args);
	}
	
}
  • 通过@Bean注解来完成对Servlet组件的注册

1.   编写Servlet (即方法一不用加上注解)

//@WebServlet(name="MyServlet",urlPatterns="/first")
public class MyServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("Hello  World!!!!!!!!!!");
	}
}

2.   编写启动类

@SpringBootApplication
//@ServletComponentScan            //该注解写上以后会自动寻找包含@WebServlet注解的类,并实例化
public class ServletSpringApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServletSpringApplication.class, args);
	}
	
	@Bean
	public ServletRegistrationBean getBean() {
		
		ServletRegistrationBean bean = new ServletRegistrationBean(new MyServlet());
		bean.addUrlMappings("/hello");
		return bean;
	}
}

俩种方法在运行后,通过访问,最后在控制台上都会打印


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-03-04 15:51:43.708  INFO 4808 --- [           main] c.example.demo.ServletSpringApplication  : Starting ServletSpringApplication on hzp-PC with PID 4808 (started by hzp in G:\eclipse\Java工程\servletSpring)
2019-03-04 15:51:43.746  INFO 4808 --- [           main] c.example.demo.ServletSpringApplication  : No active profile set, falling back to default profiles: default
2019-03-04 15:51:45.590  INFO 4808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-03-04 15:51:45.631  INFO 4808 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-04 15:51:45.631  INFO 4808 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-04 15:51:45.647  INFO 4808 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_162\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_162/bin/server;C:/Program Files/Java/jre1.8.0_162/bin;C:/Program Files/Java/jre1.8.0_162/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\MySQL\MySQL Server 5.5\bin;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;D:\eclipse\Apache\maven\bin;D:\c++\Qt5.3.1\Tools\mingw482_32\bin;D:\c++\Qt5.3.1\5.3\mingw482_32\bin;C:\Program Files\mingw-w64\x86_64-5.3.0-win32-seh-rt_v4-rev0\mingw64\bin;F:\Program Files\Git\cmd;G:\Program Files\Tortoise\bin;;G:\软件\spring-tool-suite-4-4.1.1.RELEASE-e4.10.0-win32.win32.x86_64\sts-4.1.1.RELEASE;;.]
2019-03-04 15:51:45.802  INFO 4808 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-04 15:51:45.802  INFO 4808 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1967 ms
2019-03-04 15:51:46.160  INFO 4808 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-04 15:51:46.450  INFO 4808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-04 15:51:46.454  INFO 4808 --- [           main] c.example.demo.ServletSpringApplication  : Started ServletSpringApplication in 3.435 seconds (JVM running for 3.883)
Hello  World!!!!!!!!!!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值