tags:

views:

511

answers:

3

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

+2  A: 

In Tomcat 6.0 it should be something like:

org.apache.catalina.ServerFactory.getServer().getServices

to get the services. After that you might use

Service.findConnectors

which returns a Connector which finally has the method

Connector.getPort

See the JavaDocs for the details.

jrudolph
Humm. Can a servlet access org.apache.catalina.ServerFactory ? I though that would be in the classloader of the server, and in accessible to the Servlet.
Bob Herrmann
FYI: I happen to be on Tomcat 5.5.17
Bob Herrmann
A: 

Why?

If you need during page generation for a image or css file URL, what's wrong with ServletRequest.getLocalPort() or, better yet, HttpServletRequest.getContextPath() for the whole shebang?

sblundy
Well, my application has a configureation phase in which different services are hooked up to different ports. I want to ensure that in the configuration phase, that I can verify that a service is configured to listen on a port that tomcat is accepting connections on.
Bob Herrmann
A: 

Whatever you are about to do - I'd not go down the tomcat specific road.

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.

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.

Olaf