【翻譯】瞭解spring boot 使用的嵌入式Tomcat

zetcode.com/web/embedde…java

在本教程中,咱們將展現如何使用嵌入式Tomcat服務器。web

Tomcat能夠在嵌入式模式下運行;spring

  • 這意味着不須要構建一個WAR文件並將其部署到獨立的Tomcat服務器中
  • spring boot 正是使用了嵌入式的Tomcat

本教程中的示例是用Maven構建的。apache

建立一個返回hello world的程序

maven依賴

<?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.zetcode</groupId>
    <artifactId>EmbeddedTomcatEx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <tomcat.version>9.0.0.M6</tomcat.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>         
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-juli</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
    </dependencies>    
    <build>    
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <assembleDirectory>target</assembleDirectory>
                    <programs>
                        <program>
<mainClass>com.zetcode.embedded.EmbeddedTomcatEx</mainClass>
                            <name>webapp</name>
                        </program>
                    </programs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>    
    </build>
</project>
複製代碼

這個 pom.xml 包含對嵌入式Tomcat的依賴和構建應用程序的編譯插件的依賴。 它不包含JSP的maven依賴,由於咱們只須要一個返回「hello world」字符串的servletapi

代碼示例 EmbeddedTomcatEx.java

package com.zetcode.embedded;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class EmbeddedTomcatEx {

    public static void main(String[] args) throws LifecycleException,
            InterruptedException, ServletException {

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8082);

        Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());

        Tomcat.addServlet(ctx, "Embedded", new HttpServlet() {
            @Override
            protected void service(HttpServletRequest req, HttpServletResponse resp) 
                    throws ServletException, IOException {
                
                Writer w = resp.getWriter();
                w.write("Embedded Tomcat servlet.\n");
                w.flush();
                w.close();
            }
        });

        ctx.addServletMapping("/*", "Embedded");

        tomcat.start();
        tomcat.getServer().await();
    }
}
複製代碼

EmbeddedTomcatEx是一個Java控制檯應用程序,其中main中啓動了一個嵌入式Tomcat服務器。瀏覽器

Tomcat tomcat = new Tomcat();
tomcat.setPort(8082);
複製代碼

Tomcat在端口8082上啓動。tomcat

Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());
複製代碼

每一個應用程序都映射到一個上下文。使用addContext()方法,咱們建立了一個不支持JSP文件且沒有web.xml文件的應用程序。咱們使用根上下文路徑和當前工做目錄做爲document basebash

Tomcat.addServlet(ctx, "Embedded", new HttpServlet() {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        
        Writer w = resp.getWriter();
        w.write("hello world!Embedded Tomcat servlet.\n");
        w.flush();
        w.close();
    }
});
複製代碼

使用addServlet()方法添加一個名爲Embedded的新的servlet。servlet只是用一些ASCII文本進行響應。服務器

ctx.addServletMapping("/*", "Embedded");
複製代碼

添加一個對Embedded訪問映射。"/*"指訪問本機8082端口中的任何url都會調用Embeddedapp

tomcat.start();
tomcat.getServer().await();
複製代碼

啓動Tomcat服務器。

測試

這裏在終端用curl進行測試,也能夠用瀏覽器測試

$ curl localhost:8082/
複製代碼

終端將會輸出hello world字符串

hello world!Embedded Tomcat servlet.
複製代碼
相關文章
相關標籤/搜索