1. 程式人生 > >springboot整合servlet兩種方式

springboot整合servlet兩種方式

前言:

           springboot與servlet整合的兩種方式,整合步驟如下:

springboot整合servlet方式一:

專案結構:

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>

    <groupId>com.lucifer.springboot</groupId>
    <artifactId>springboot-servlet</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-servlet</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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>
SpringbootServletApplication:
package com.lucifer.springboot;

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


/**
 * @ServletComponentScan
 * 在springboot啟動時會掃描@WebServlet,並將該類例項化
 */
@SpringBootApplication
@ServletComponentScan
public class SpringbootServletApplication {

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

啟動專案,瀏覽器輸入localhost:8080/first,

ps:整合過程中新手很容易遇坑的地方:原本我的專案結構是這樣的,如下圖:

這時候啟動專案,控制檯無報錯,瀏覽器輸入地址,404,原因就是因為com.lucifer.springboot.servlet這個包沒有掃描到。

 那百度了,按照百度查到的資料,加了掃描包的註解,沒用,效果依然涼涼。

因此這裡就把啟動類放在com.lucifer.springboot包下,FirstServet就包在子包com.lucifer.springboot.servlet下,啟動類的掃描預設是掃描與該啟動類同包以及其子包下的類。

springboot整合servlet方式二:

專案結構:

SecondServlet:
package com.lucifer.springboot.servlet;

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

/**
 * @author: Lucifer
 * @create: 2018-09-19 14:39
 * @description:
 **/
public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().append("springboot整合servlet成功");
    }
}
SpringbootServletApplication:
package com.lucifer.springboot;

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



@SpringBootApplication
public class SpringbootServletApplication {

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

    @Bean
    public ServletRegistrationBean getservletRegistrationBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }

}