File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes get port number in tomcat 7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "get port number in tomcat 7" Watch "get port number in tomcat 7" New topic

get port number in tomcat 7

Angei Angie

Joined: Feb 05, 2013
Posts: 1

I have currently moved to tomcat 7 where i need to access the port .A sample code which wrks fine on tomcat 6
Server server = ServerFactory.getServer();
Service[] services = server.findServices();
for (Service service : services) {
for (Connector connector : service.findConnectors()) {
ProtocolHandler protocolHandler = connector.getProtocolHandler();
if (protocolHandler instanceof Http11Protocol
|| protocolHandler instanceof Http11AprProtocol
|| protocolHandler instanceof Http11NioProtocol) {
serverPort = connector.getPort();
System.out.println("HTTP Port: " + connector.getPort());


is there something similar in tomcat 7 ?
I tried this but this aint working either ..
import org.apache.catalina.Server;

MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");

Thanks in advance

I agree. Here's the link:
subject: get port number in tomcat 7
It's not a secret anymore!