我需要知道我的 java webapp 在 webapp 启动时运行的 HTTP 和 HTTPS 端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7481432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:10:38  来源:igfitidea点击:

I need to know the HTTP and HTTPS port my java webapp is running on webapp startup

javatomcatservlets

提问by Basil Musa

Is it possible to find out the HTTP and HTTPS ports configured for a Tomcat webserver from the web application's java code before any http or https requests take place.

在任何 http 或 https 请求发生之前,是否可以从 Web 应用程序的 java 代码中找出为 Tomcat Web 服务器配置的 HTTP 和 HTTPS 端口。

I need this information on application startup. I don't want to wait for someone to initiate an HTTP request and call getServerPort().

我需要有关应用程序启动的信息。我不想等待某人发起 HTTP 请求并调用 getServerPort()。

What I want is to figure out HTTP and HTTPS ports on startup of the webapplication.

我想要的是在 web 应用程序启动时找出 HTTP 和 HTTPS 端口。

Is this possible? I searched very well on this problem but hardly found any solutions.

这可能吗?我对这个问题进行了很好的搜索,但几乎没有找到任何解决方案。

采纳答案by pap

To get access to this configuration at runtime, one way is to create your own Valve, extended from ValveBaseand register it in the server.xml configuration (see http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html) under your Engine. Override the setContainer(Container container)method. If registered under the Engine, the containerparameter should be of type StandardEngine. From that, you can call getService()to get a reference to the Service. The service has a method findConnectors(). That returns an array of Connectorinstances, reflecting the configured connectors (endpoints) for your service. From them, you can get the configured port by calling getPort().

要在运行时访问此配置,一种方法是创建您自己的 Valve,从ValveBaseserver.xml 配置扩展并注册它(参见http://tomcat.apache.org/tomcat-7.0-doc/config/valve .html) 在您的Engine. 覆盖该setContainer(Container container)方法。如果在 Engine 下注册,则container参数的类型应为StandardEngine。从那里,您可以调用getService()以获取对Service. 该服务有一个方法findConnectors()。这将返回一组Connector实例,反映为您的服务配置的连接器(端点)。从他们那里,您可以通过调用getPort().

You will need to have catalina.jar on your build classpath. Note, this is invoked at server startup so you'll have to store the port information in some globally available storage if you need access to it later.

你需要在你的构建类路径上有 catalina.jar。请注意,这是在服务器启动时调用的,因此如果您以后需要访问它,您必须将端口信息存储在一些全局可用的存储中。

If you don't want to do it in a valve, things get a bit dirtier as you'll have to use introspection and break field visibility containment.

如果您不想在阀门中这样做,事情会变得有点脏,因为您必须使用自省并打破现场可见性限制。

This is a sample of a standard filter that extracts port information in the init()method

这是在init()方法中提取端口信息的标准过滤器示例

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.catalina.Container;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.commons.lang3.reflect.FieldUtils;

public class TestFilter implements Filter {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        arg2.doFilter(arg0, arg1);

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        ServletContext ctx = arg0.getServletContext();

        try {
            Object o = FieldUtils.readField(ctx, "context", true);
            StandardContext sCtx = (StandardContext) FieldUtils.readField(o, "context", true);
            Container container = (Container) sCtx;

            Container c = container.getParent();
        while (c != null && !(c instanceof StandardEngine)) {
            c = c.getParent();
        }

        if (c != null) {
            StandardEngine engine = (StandardEngine) c;
            for (Connector connector : engine.getService().findConnectors()) {
                // Get port for each connector. Store it in the ServletContext or whatever
                System.out.println(connector.getPort());
            }
        }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}

It requires commons-lang3 (for the FieldUtils class).

它需要 commons-lang3(对于 FieldUtils 类)。

回答by aldrin

In the conf/server.xml, in the Connector configuration.

在 conf/server.xml 中,在连接器配置中。

Here is a sample,

这是一个示例,

<!-- A "Connector" represents an endpoint by which requests are received
     and responses are returned. Documentation at :
     Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
     Java AJP  Connector: /docs/config/ajp.html
     APR (HTTP/AJP) Connector: /docs/apr.html
     Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" />