springBoot整合servlet的方式(基于maven)

pom.xml

<?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>1.5.10.RELEASE</version>
    </parent>
    <packaging>jar</packaging>
    <groupId>com.bjsxt</groupId>
    <artifactId>02SpringBoot_Servlet</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
</dependencies>
</project>

springboot的父类

 
        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
    
springboot的启动器:其实就是一些jar包的集合
   
        org.springframework.boot
        spring-boot-starter-web
        2.2.0.RELEASE
    

整合方式一:扫描注解来注册servlet组件
代码:

package com.bjsxt.servlet;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * springboot整合servlet方式通过扫描注解注册servlet组件
 */
@WebServlet(name = "FilrstServlet",urlPatterns = "/first")
public class FilrstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("整合方式一");

    }
}

启动类:

package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;


@SpringBootApplication
@ServletComponentScan//springboot启动时去扫描@WebServlet注解,并将该类实力恶化
public class StartApp {
    public static void main(String[] args) {
        SpringApplication.run(StartApp.class,args);
    }
}

springBoot整合servlet方式二:根据方法

package com.bjsxt.servlet;

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

/**
 * springboot整合servlet方式通过方法注册servlet组件
 */

public class SecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("整合方式二");

    }
}

启动类:

package com.bjsxt;

import com.bjsxt.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * 通过方法整合servlet
 */
@SpringBootApplication
public class StartApp2 {
    public static void main(String[] args) {
        SpringApplication.run(StartApp2.class,args);
    }
    //springboot启动后执行 @Bean
    @Bean
    public ServletRegistrationBean  getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
}

你可能感兴趣的:(Java,springBoot)