Question

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

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.

Was it helpful?

Solution

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.

OTHER TIPS

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top