代码适用于嵌入式Apache Tomcat 8,但不适用于9.更改了什么? [英] Code works with Embedded Apache Tomcat 8 but not with 9. What's changed?

查看:139
本文介绍了代码适用于嵌入式Apache Tomcat 8,但不适用于9.更改了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Apache Tomcat嵌入到eclipse Web应用程序项目中.
当我将最新的Tomcat 8(嵌入式8.0.8)罐子用作依赖项时,该代码有效,并且该服务器的响应地址为 http://localhost:8080 ,但是,它无法以相同的方式启动,并且在使用最新的Tomcat 9(9.0.5 Embedded)jar时在此地址中没有响应.
代码很简单.我已经进行了尽可能详尽的研究,但没有弄清楚出什么问题了.

Embedding Apache Tomcat into an eclipse web app project.
The code works when I'm using the latest Tomcat 8 (8.0.5 Embedded) jars as dependencies, and this server responds at http://localhost:8080, however, it fails to start the same way and does not respond in this address when using the latest Tomcat 9's (9.0.5 Embedded) jars.
The code is very simple. I've researched as thoroughly as I could but didn't figure out what's wrong.

package app;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class Main {

    public static void main(String[] args) {

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

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

        tomcat.getServer().await();
    }

}

使用Tomcat 9.0.5嵌入式jar时的控制台输出:

console output when using Tomcat 9.0.5 Embedded jars:

org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]

使用Tomcat 8.0.5嵌入式jar时的控制台输出:

console output when using Tomcat 8.0.5 Embedded jars:

org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]

推荐答案

似乎您尚未向嵌入式服务器添加Connector. Tomcat 9不再自动为您的服务器添加Connector,因此您必须自己触发它:

It looks like you haven't added a Connector to your embedded server. Tomcat 9 no longer automatically adds a Connector to your server for you, so you'll have to trigger it yourself:

package app;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class Main {

    public static void main(String[] args) {

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.getConnector(); // Trigger the creation of the default connector

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

        tomcat.getServer().await();
    }
}

值得一提的是,对早期版本的Tomcat来说,添加对tomcat.getConnector()的调用也应该是安全的,因此这不必是仅限Tomcat 9"的事情.

It's worth mentioning that adding a call to tomcat.getConnector() should be safe for previous versions of Tomcat as well, so this need not be a "Tomcat 9-only" thing.

这篇关于代码适用于嵌入式Apache Tomcat 8,但不适用于9.更改了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆