我需要知道我的java webapp在webapp启动时运行的HTTP和HTTPS端口 [英] I need to know the HTTP and HTTPS port my java webapp is running on webapp startup

查看:215
本文介绍了我需要知道我的java webapp在webapp启动时运行的HTTP和HTTPS端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发出任何http或https请求之前,是否可以从Web应用程序的java代码中找到为Tomcat 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在应用程序启动时需要此信息。我不想等待某人发起HTTP请求并调用getServerPort()。

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

我想要的是在启动webapplication时弄清楚HTTP和HTTPS端口。

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.

推荐答案

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

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

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

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.

这是一个标准的样本过滤器,用于在 init()方法中提取端口信息

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


    }

}

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

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

这篇关于我需要知道我的java webapp在webapp启动时运行的HTTP和HTTPS端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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