博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot整合Servlet使用
阅读量:2144 次
发布时间:2019-04-30

本文共 4332 字,大约阅读时间需要 14 分钟。

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

  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: default2019-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 WebApplicationContext2019-03-04 15:51:45.802  INFO 4808 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1967 ms2019-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!!!!!!!!!!

 

转载地址:http://rwegf.baihongyu.com/

你可能感兴趣的文章
Leetcode C++ 《第175场周赛-1 》5332.检查整数及其两倍数是否存在
查看>>
Leetcode C++ 《第175场周赛-2 》5333.制造字母异位词的最小步骤数
查看>>
Leetcode C++ 《第175场周赛-3》1348. 推文计数
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-45》338.比特位计数
查看>>
读书摘要系列之《kubernetes权威指南·第四版》第一章:kubernetes入门
查看>>
Leetcode C++《热题 Hot 100-46》739.每日温度
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
[Kick Start 2020] Round A 1.Allocation
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>
阿里云《云原生》公开课笔记 第一章 云原生启蒙
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>