<p>In Tomcat 5.5 the server.xml can have many connectors, typically port only 8080, but for my application a user might configure their servlet.xml to also have other ports open (say 8081-8088).  I would like for my servlet to figure out what socket connections ports will be vaild (During the Servlet.init() tomcat has not yet started the connectors.) </p>

<p>I could find and parse the server.xml myself (grotty), I could look at the thread names (after tomcat starts up - but how would I know when a good time to do that is? )  But I would prefer a solution that can execute in my servlet.init() and determine what will be the valid port range.   Any ideas?   A solution can be tightly bound to Tomcat for my application that's ok.</p>

[{'answer_id': 65656, 'author': 'jrudolph', 'author_id': 7647, 'author_profile': 'https://Stackoverflow.com/users/7647', 'pm_score': 3, 'selected': True, 'text': '<p>In Tomcat 6.0 it should be something like:</p>\n\n<pre><code>org.apache.catalina.ServerFactory.getServer().getServices \n</code></pre>\n\n<p>to get the services. After that you might use </p>\n\n<pre><code>Service.findConnectors\n</code></pre>\n\n<p>which returns a Connector which finally has the method</p>\n\n<pre><code>Connector.getPort\n</code></pre>\n\n<p>See the <a href="http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/connector/Connector.html" rel="nofollow noreferrer">JavaDocs</a> for the details.</p>\n'}, {'answer_id': 65699, 'author': 'sblundy', 'author_id': 4893, 'author_profile': 'https://Stackoverflow.com/users/4893', 'pm_score': 0, 'selected': False, 'text': '<p>Why?</p>\n\n<p>If you need during page generation for a image or css file URL, what\'s wrong with <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getLocalPort()" rel="nofollow noreferrer">ServletRequest.getLocalPort()</a> or, better yet, <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html#getContextPath()" rel="nofollow noreferrer">HttpServletRequest.getContextPath()</a> for the whole shebang?</p>\n'}, {'answer_id': 113551, 'author': 'Olaf Kock', 'author_id': 13447, 'author_profile': 'https://Stackoverflow.com/users/13447', 'pm_score': 0, 'selected': False, 'text': "<p>Whatever you are about to do - I'd not go down the tomcat specific road.</p>\n\n<p>If you really need to locate different ports, configure them to your webapp through the usual configuration means - e.g. specifying  values. You'd not have any automatic discovery, but also it won't break on tomcats next update.</p>\n\n<p>More specifically, I'd say that I believe you've asked the wrong question. E.g. you have your requirement, opted for one solution and asked for how to implement this solution. I believe you'd get better answers if you stated your first hand requirement and asked for a solution for this.</p>\n"}]
['https://Stackoverflow.com/questions/65530', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6580/']