如何在SpringBoot当中去整合我们的Servlet,SpringBoot在整合Servlet时,提供了两种整合方式,第一种是通过注解扫描,来完成Servlet组件的注册,第二种方式呢,通过方法完成Servlet组件的注册,那我们先来看第一种方式,首先我们要创建一个SpringBoot的项目,项目类型仍然是jar类型,还要田间父工程的信息,然后我们用的还是1.5.12.RELEASE,这样我们 一个项目就构建好了,我们要将JDK的版本修改为1.8,那么我们这个时候就要用到properties,然后java.version<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version>
</properties>接下来我们要将WEB启动器的坐标,添加到我们的pom文件当中,这个时候会讲jar包添加到我们的项目当中,接下来我们就可以创建我们的Servlet@WebServlet(name="FirstServlet",urlPatterns="/first")
public class FirstServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("FirstServlet............");}
}这个时候我们村重写Servlet的doGet方法,这样我们的一个Servlet就填写完了,我们先回顾一下,我们以往在做Servlet开发时,当我们创建好一个Servlet,我们要在web.xml里对Servlet做配置,首先要在web.xml里面添加一个servlet节点,在servlet节点当中呢,servlet-name,在servlet-name当中呢,这是我们以往要做的一些配置,在这里我们没有web.xml,我们没有办法去做这样的一个配置,在SpringBoot当中,提供了一个叫WebServlet的Annotation,在这个Annotation当中呢,我们可以去完成对应的Servlet配置,用到了一个name,和urlPatterns,这个name属性,其实就是我们web.xml配的servlet-name,只是我们这里不再需要对servlet-class做配置,因为你在哪个class上添加了servlet,则表示这个class就是一个servlet,这样我们一个servlet就配置完了,第二步编写启动类,在这里我们编写一个SpringBoot的启动类/*** SpringBoot整合Servlet方式一**/
@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}但是我们现在要让SpringBoot在启动的时候,去扫描我们servlet当中,增加的WebServlet注解,怎么办呢,我们要在这加一个Annotation,叫@ServletComponentScan,这个注解表示什么含义呢,在SpringBoot启动时,会扫描WebServlet的信息,并将该类实例化,这个就是Annotation的一个作用,那么这里我们要注意的就是,他就会去扫描这个Annotation,那么接下来我们把Servlet启动一下,如果控制台输出这个信息,表示servlet是可运行的,我们打开浏览器localhost:8080/first这个servlet已经执行了,这种方式就是通过,扫描注解的方式完成组件的注册,我们再来看第二种方式,通过方法,通过方法来完成组件的注册,首先我们还是得编写一个servlet/***SpringBoot整合Servlet方式二**/
public class SecondServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("SecondServlet..........");}}那么这种方式和第一种方式不同的是什么呢,我们不需要在Servlet的client上,添加WebServlet的Annotation,就是一个普通的Servlet就行了,然后我们去编写一个启动类,/*** SpringBoot整合Servlet方式二**/
@SpringBootApplication
public class App2 {public static void main(String[] args) {SpringApplication.run(App2.class, args);}@Beanpublic ServletRegistrationBean getServletRegistrationBean(){ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());bean.addUrlMappings("/second");return bean;}
}那么我们在第二个servlet当中,不需要添加WebServlet Annotation,就意味着在我们的启动器当中,也不需要@ServletComponentScan这个注解了,那么我们怎么办呢,这里我们需要添加一个方法,这个方法叫什么名倒无所谓,但是这个方法呢,他的返回值必须是ServletRegistrationBean,这个类型的一个对象,然后我们随便起个名,getServletRegistrationBean,那么这个对象其实就是来注册servlet的一个对象,在这个对象的构造方法当中,new我们的servlet,然后我们看一下在我们的servlet当中,把原来的Annotation去掉了,那我们怎么来指定servlet的url,pattern的配置呢,在ServletRegistrationBean下,提供了一个方法,addUrlMappings,然后将这个对象返回,接下来我们还要加一个@Bean的注解,然后我们去执行启动器,这样的启动器在启动时,通过ServletRegistrationBean去完成servlet的注册,这样就方便处理请求了,在这里先去启动我们的SpringBootlocalhost:8080/second
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>02-spring-boot-servlet</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
</project>
package com.learn.servlet;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;/***SpringBoot整合Servlet方式一**<servlet>*  <servlet-name>FirstServlet</servlet-name>*  <servlet-class>com.learn.servlet.FirstServlet</servlet-class>*</servlet>**<servlet-mapping>* <servlet-name>FirstServlet</servlet-name>* <url-pattern>/first</url-pattern>*</servlet-mapping>**/
@WebServlet(name="FirstServlet",urlPatterns="/first")
public class FirstServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("FirstServlet............");}
}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;/*** SpringBoot整合Servlet方式一**/
@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}
package com.learn.servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/***SpringBoot整合Servlet方式二**/
public class SecondServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("SecondServlet..........");}}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;import com.learn.servlet.SecondServlet;/*** SpringBoot整合Servlet方式二**/
@SpringBootApplication
public class App2 {public static void main(String[] args) {SpringApplication.run(App2.class, args);}@Beanpublic ServletRegistrationBean getServletRegistrationBean(){ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());bean.addUrlMappings("/second");return bean;}
}

springboot整合servlet相关推荐

  1. springmvc整合redis_111 SpringBoot整合Servlet JSP Filter Listener

    1. SpringBoot介绍 (1) 什么是Spring Boot? (2) Spring Boot有哪些特点? Spring Boot 设计目的是用来简化新 Spring 应用的初始搭建以及开发过 ...

  2. springboot 整合 Servlet、Filter、Listener、访问静态资源

    springboot 整合 Servlet.Filter.Listener.访问静态资源 1.引入pom.xml依赖 <dependency><groupId>org.spri ...

  3. 002_SpringBoot整合Servlet

    1. 通过注解扫描完成Servlet组件的注册(SpringBoot整合Servlet方式一) 1.1. 使用maven构建SpringBoot的名叫spring-boot-servlet项目 1.2 ...

  4. springboot整合filter

    如何在SpringBoot当中去整合Filter,SpringBoot在整合Filter时,其实和整合Servlet差不多,提供了两种方案,一种还是通过注解扫描,完成Filter的注册,第二种方式呢, ...

  5. 第三篇:Spring Boot整合Servlet

    一.Springboot整合Servlet 第一种方案: 1.创建一个自定义的servlet,继承HttpServlet添加@WebServlet注解 以前ssm中的web.xml配置文件中的serv ...

  6. 整合servlet、整个filter、整合listener、文件上传

    一,整合 Servlet 通过注解扫描完成 Servlet 组件的注册 1.1 编写 servlet /** *SpringBoot 整合 Servlet 方式一 * *<servlet> ...

  7. SpringBoot再回首:SpringBoot之Servlet用法

           今天我们来讨论一下,SpringBoot怎么整合Servlet....       在传统的Servlet开发时,我们创建一个Servlet,.需要在web.xml里做配置,相信大家在刚 ...

  8. SpringBoot(四)-- 整合Servlet、Filter、Listener

    SpringBoot中有两种方式可以添加 Servlet.Filter.Listener. 1.代码注册 通过ServletRegistrationBean. FilterRegistrationBe ...

  9. 玩转 SpringBoot 2 快速整合 Servlet

    前言 本文主要介绍如何在SpringBoot 2 中使用 Servlet 的快速搭建教程,阅读前需要你必须了解 Servlet 的基础使用以及如何搭建 SpringBoot 项目. 快速演示操作 第一 ...

最新文章

  1. 深度学习概述:NLP vs CNN
  2. 用看板工具Leangoo思维导图实现影响地图
  3. table和div在页面布局上应该注意的问题
  4. php浮点数计算比较及取整不准确解决方法
  5. jzoj6375-华灵「蝶妄想」【结论题】
  6. 十二月份找工作好找吗_小儿推拿师工作好找吗?工资高吗?
  7. 谷歌Pixel 6系列手机发布会官宣定档 10月19日发布
  8. 一张图看懂字节跳动8年创业史,太励志了吧
  9. php uid gid,用户信息,函数介绍,PHP开源CMS系统帮助文档
  10. sql server php删除,使用 SQL Server 添加删除修改查询储存过程
  11. 6.4-全栈Java笔记:异常处理办法(下)
  12. 【滤波器】7. 带通滤波器
  13. Unity3D眼镜Shader技术实现
  14. oracle12c不使用cdb模式,oracle 12c non-cdb升级成cdb模式
  15. Student Alcohol Consumption学生酒类消费
  16. k8s——pv(静态+动态storageclass)与pvc
  17. 什么是web前端技术?要学什么?
  18. 音视频dsp中对音频的处理
  19. 蓝屏代码:DRIVR_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS
  20. android usb挂载分析---MountService启动

热门文章

  1. 最全面的 MySQL 索引详解
  2. [Leetcode] Binary Tree Maximum Path Sum
  3. 搞定ubuntu下环境变量的配置
  4. 迎合人工智能时代 码教授开设Python课程
  5. 2018集训队日常训练1
  6. CountVectorizer,Tf-idfVectorizer和word2vec构建词向量的区别
  7. Chapter 1 First Sight——33
  8. 用PHP实现单向链表
  9. ASP.NET MVC 随笔汇总
  10. 编辑器FreeTextBox升级至3.0