I need to know the HTTP and HTTPS port that java webapp is running when webapp starts

Is it possible to find out the HTTP and HTTPS ports configured for the Tomcat web server from the Java code of the web application before any http or https requests are made.

I need this information on launching the application. I do not want to wait until someone starts an HTTP request and calls getServerPort ().

I want to find out HTTP and HTTPS ports when starting a web application.

Is it possible? I searched very well for this problem, but hardly found any solutions.

+8
java tomcat servlets
source share
3 answers

To access this configuration at runtime, one way is to create your own Valve extended from ValveBase and register it in server.xml (see http://tomcat.apache.org/tomcat-7.0-doc/config/valve .html ) under your Engine . Override the setContainer(Container container) method. If it is registered under the engine, the container parameter must be of type StandardEngine . From this, you can call getService() to get a link to the Service . The service has a findConnectors() method. This 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 for your classpath class. Please note that this is called when the server starts, so you will need to store the port information in some globally accessible storage if you need access to it later.

If you donโ€™t want to do this in the valve, things get a little dirtier, since you have to use introspection and turn off field visibility.

This is a sample standard filter that retrieves 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).

+5
source
0
source

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

Here is an example

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

All Articles