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

发布于 2024-12-05 08:36:39 字数 244 浏览 1 评论 0原文

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

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

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

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

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.

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

用心笑 2024-12-12 08:36:39

要在运行时访问此配置,一种方法是创建自己的 Valve,从 ValveBase 并将其注册到 server.xml 配置中(请参阅http://tomcat.apache.org/tomcat-7.0-doc/config/valve .html)在您的引擎下。重写 setContainer(Container container) 方法。如果在引擎下注册,container 参数的类型应为 StandardEngine。由此,您可以调用 getService() 来获取对 服务。该服务有一个方法 findConnectors()。返回一个 Connector 数组 实例,反映为您的服务配置的连接器(端点)。从它们中,您可以通过调用 getPort() 来获取配置的端口。

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

如果您不想在阀门中执行此操作,事情会变得有点脏,因为您必须使用内省并打破字段可见性遏制。

这是在 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();
        }


    }

}

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

To get access to this configuration at runtime, one way is to create your own Valve, extended from ValveBase and 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 container parameter 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 Connector instances, reflecting the configured connectors (endpoints) for your service. From them, you can get the configured port by calling 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.

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

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).

月亮邮递员 2024-12-12 08:36:39

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

这是一个样本,

<!-- 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" />

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

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文