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.
A Java web application is a collection of dynamic resources (such as Servlets, JavaServer Pages, Java classes and jars) and static resources (HTML pages and pictures). A Java web application can be deployed as a WAR (Web ARchive) file.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With