[Spring Boot] 使用多个Servlet

本文介绍了如何在Spring Boot应用中配置Servlet、Filter和监听器。通过示例展示了使用ServletRegistrationBean注册DispatcherServlet及自定义Servlet的方法,并说明了如何利用@ServletComponentScan自动注册组件。

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

当使用Spring boot的嵌入式servlet容器时,可以通过Spring bean或扫描Servlet组件的方式注册Servlet、Filter和Servlet规范的所有监听器(例如HttpSessionListener)

  • 当urlMapping不是很复杂时,可以通过ServletRegistrationBeanFilterRegistrationBean 和 ServletListenerRegistrationBean获得完整控制。如果bean实现了ServletContextInitializer接口的话则可以直接注册。
  • 当使用@ServletComponentScan扫描Servlet组件时,Servlet、过滤器和监听器可以是通过@WebServlet@WebFilter@WebListener自动注册
Application.java

 

package com.yqu.multiservlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.DispatcherServlet;

@SpringBootApplication
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

  @Bean
  public ServletRegistrationBean dispatcherRegistration(
      DispatcherServlet dispatcherServlet) {
    ServletRegistrationBean registration =
        new ServletRegistrationBean(dispatcherServlet);
    registration.addUrlMappings("/hirest/*");
    printStacks();
    return registration;
  }

  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    printStacks();
    return new ServletRegistrationBean(
        new SigninServlet(), "/signin");
  }

  private void printStacks() {
    StackTraceElement[] elements = Thread.currentThread().getStackTrace();
    System.out.println("========================");

    for (int i = 0; i < elements.length; i++) {
      System.out.println(elements[i]);
    }
  }
}
SigninServlet.java
package com.yqu.multiservlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SigninServlet extends HttpServlet {
  public void init(ServletConfig config)
      throws ServletException {
    super.init(config);

  }

  protected void doGet(
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException {
    response.sendRedirect("http://blog.sina.com.cn/yandongqu");
  }
}
HelloController.java
package com.yqu.multiservlet;

import org.springframework.hateoas.ResourceSupport;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

@RestController
public class HelloController {
  @RequestMapping(value = "/", method = RequestMethod.GET)
  @ResponseBody
  public HttpEntity  home() {
    ResourceSupport home = new ResourceSupport();
    home.add(linkTo(methodOn(HelloController.class).home()).withSelfRel());
    return new ResponseEntity(home, HttpStatus.OK);
  }
}
application.properties
server.context-path=/HelloMultiServlet
server.port=8080

applicationDefaultJvmArgs: [
    "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=55558"
]
build.gradle
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'hello-multiservlet'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("com.fasterxml.jackson.core:jackson-databind")
    compile("org.springframework.hateoas:spring-hateoas")
    compile("org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE")
    compile("com.jayway.jsonpath:json-path:0.9.1")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

测试

  • 通过REST访问http://localhost:8080/HelloMultiServlet/hirest/

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值