Spring Boot使用Servlet及Filter过程详解

在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式。若使用的是Servlet3.0+版本,则两种方式均可使用;若使用的是Servlet2.5版本,则只能使用配置类方式

一、Servlet3.0+版本方式

(1)创建工程07-servlet

(2)导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>com.abc</groupId>
  <artifactId>07-servlet</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <!--热部署依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

(3)创建Servlet

(4)定义Filter

(5) 修改入口类

在入口类中添加Servlet扫描注解

(6) 测试

http://localhost:8080/some

二、Servlet2.5版本方式

(1)创建工程07-servlet2

复制07-servlet,命名07-servlet2

(2)导入依赖

无需修改

(3) 定义Servlet

(4)定义Filter

(5)定义配置类

(6) 修改入口类

(7)测试

http://localhost:8080/some

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 详解在spring boot中配置多个DispatcherServlet

    spring boot为我们自动配置了一个开箱即用的DispatcherServlet,映射路径为'/',但是如果项目中有多个服务,为了对不同服务进行不同的配置管理,需要对不同服务设置不同的上下文,比如开启一个DispatcherServlet专门用于rest服务. 传统springMVC项目 在传统的springMVC项目中,配置多个DispatcherServlet很轻松,在web.xml中直接配置多个就行: <servlet> <servlet-name>restServle

  • Spring Boot实现异步请求(Servlet 3.0)

    在spring 3.2 及以后版本中增加了对请求的异步处理,旨在提高请求的处理速度降低服务性能消耗. 在我们的请求中做了耗时处理,当并发请求的情况下,为了避免web server的连接池被长期占用而引起性能问题,调用后生成一个非web的服务线程来处理,增加web服务器的吞吐量. 为此 Servlet 3.0 新增了请求的异步处理,Spring 也在此基础上做了封装处理. 本文还是以代码例子的方式说明如何在 Spring Boot 中应用异步请求. 首先说一下几个要点: 1.@WebFilter

  • Spring Boot 编写Servlet、Filter、Listener、Interceptor的方法

    前言 在编写过滤器.监听器.拦截器之前我们需要在spring-boot启动的类上加上注解@ServletComponentScan: @SpringBootApplication @ServletComponentScan public class MySpringbootApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootApplication.class, args)

  • 深入讲解spring boot中servlet的启动过程与原理

    前言 本文主要介绍了关于spring boot中servlet启动过程与原理的相关内容,下面话不多说了,来一起看看详细的介绍吧 启动过程与原理: 1 spring boot 应用启动运行run方法 StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; FailureAnalyzers analyzers = null; configureHe

  • SpringBoot初始教程之Servlet、Filter、Listener配置详解

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar包的形式,这种情况下如何解决?SpringBoot 提供了两种方案进行解决 2.快速开始 2.1 方案一 方案一采用原生Servlet3.0的注解进行配置.@WebServlet .@WebListener.@WebFilter是Servlet3.0 api中提供的注解 通过注解可以完全代替web

  • Spring Boot 中的Servlet简单使用

    当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet.Filter和Servlet规范的所有监听器(如HttpSessionListener监听器). Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为"/".也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢? 在spring boot中添加自己的Servlet有两种方法,代码注册Servle

  • springboot注入servlet的方法

    问:有了springMVC,为什么还要用servlet?有了servlet3的注解,为什么还要使用ServletRegistrationBean注入的方式? 使用场景:在有些场景下,比如我们要使用hystrix-dashboard,这时候就需要注入HystrixMetricsStreamServlet(第三方的servlet),该servlet是hystrix的组件. 一.代码 1.TestServlet(第一个servlet) package com.xxx.secondboot.servle

  • springboot扫描自定义的servlet和filter代码详解

    这几天使用spring boot编写公司一个应用,在编写了一个filter,用于指定编码的filter,如下: /** * Created by xiaxuan on 16/11/1. */ @WebFilter(urlPatterns = "/*",filterName="CharacterEncodeFilter", initParams={ @WebInitParam(name="encoding",value="UTF-8&qu

  • Spring Boot使用Servlet及Filter过程详解

    在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用配置类方式 一.Servlet3.0+版本方式 (1)创建工程07-servlet (2)导入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apa

  • Spring Boot整合web层实现过程详解

    Spring Boot中对Spring MVC的文件上传是一脉相传的,我们双击shift去搜CommonsMultipartResolver这个类,它是文件上传的一个实现类.我们先看一下源码: 我们可以看到它是MultipartResolver的实现类,我们再Ctrl+H,就可以看到右侧MultipartResolver的两个实现类.第一个实现类在servlet3.0之后,什么都不用加,就可以直接使用.第二个实现类的兼容性要好一些,早期的servlet也可以使用,但需要自己额外的加依赖.那么在S

  • Spring boot项目使用thymeleaf模板过程详解

    在spring boot 项目中使用thymeleaf模板,将后台数据传递给前台界面. 1.将后台数据传递给前台有很多种方式,可以将后台要传递的数据转换成json格式,去传递给前台,也可以通过model形式去传递出去,这篇博客主要是使用thymeleaf模板,将后台数据传递给前台. 2.首先要在spring boot 项目中添加如下依赖: <dependency> <groupId>org.springframework.boot</groupId> <artif

  • Spring Boot Admin Server管理客户端过程详解

    要通过Spring Boot Admin Server监视和管理微服务应用程序,应该添加Spring Boot Admin启动器客户端依赖项,并将Admin Server URI指向应用程序属性文件. 注 - 要监视应用程序,应为微服务应用程序启用Spring Boot Actuator端点. 首先,在构建配置文件中添加以下Spring Boot Admin启动程序客户端依赖项和Spring Boot启动程序执行程序依赖项. Maven用户可以在pom.xml 文件中添加以下依赖项 - <dep

  • Spring Boot Async异步执行任务过程详解

    异步调用就是不用等待结果的返回就执行后面的逻辑,同步调用则需要等带结果再执行后面的逻辑. 通常我们使用异步操作都会去创建一个线程执行一段逻辑,然后把这个线程丢到线程池中去执行,代码如下: ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.execute(() -> { try { // 业务逻辑 } catch (Exception e) { e.printStackTrace(

  • Spring Boot自定义错误视图的方法详解

    Spring Boot缺省错误视图解析器 Web应用在处理请求的过程中发生错误是非常常见的情况,SpringBoot中为我们实现了一个错误视图解析器(DefaultErrorViewResolver).它基于一些常见的约定,尝试根据HTTP错误状态码解析出错误处理视图.它会在目录/error下针对提供的HTTP错误状态码搜索模板或者静态资源,比如,给定了HTTP状态码404,它会尝试搜索如下模板或者静态资源: /<templates>/error/404.<ext> - 这里<

  • spring boot application properties配置实例代码详解

    废话不多说了,直接给大家贴代码了,具体代码如下所示: # =================================================================== # COMMON SPRING BOOT PROPERTIES # # This sample file is provided as a guideline. Do NOT copy it in its # entirety to your own application. ^^^ # ========

  • Spring Boot读取resources目录文件方法详解

    这篇文章主要介绍了Spring Boot读取resources目录文件方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在Java编码过程中,我们常常希望读取项目内的配置文件,按照Maven的习惯,这些文件一般放在项目的src/main/resources下,因此,合同协议PDF模板.Excel格式的统计报表等模板的存放位置是resources/template/test.pdf,下面提供两种读取方式,它们分别在windows和Linux

  • Spring Boot thymeleaf模板引擎的使用详解

    在早期开发的时候,我们完成的都是静态页面也就是html页面,随着时间轴的发展,慢慢的引入了jsp页面,当在后端服务查询到数据之后可以转发到jsp页面,可以轻松的使用jsp页面来实现数据的显示及交互,jsp有非常强大的功能,但是,在使用springboot的时候,整个项目是以jar包的方式运行而不是war包,而且还嵌入了tomcat容器,因此,在默认情况下是不支持jsp页面的.如果直接以纯静态页面的方式会给我们的开发带来很大的麻烦,springboot推荐使用模板引擎. 模板引擎有很多种,jsp,

随机推荐