Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the server port number from tomcat without a request

Is there any Tomcat API or configuration available which can tell an application (probably on startup), what port its running on without a request?

Imagine a scenario where there are two web applications running in the same Tomcat and one of which need to invoke a web service from the other one. We don't want the request to leave the Tomcat (if you use the Apache server name or absolute URL, the request will go out and come back again and it can go to any instance) and come back in. For that I know the name of the machine but no way to get the port number. I know I can hard code this information but I don't want to do this as I want my war file to be application server agnostic.

I know that we can find it if we have a HTTPServletRequest

This works only for Tomcat 6 and will not work on Tomcat 7

like image 371
Teja Kantamneni Avatar asked Oct 05 '10 19:10

Teja Kantamneni


People also ask

How do I find my Tomcat port number?

Use a browser to check whether Tomcat is running on URL http://localhost:8080 , where 8080 is the Tomcat port specified in conf/server. xml. If Tomcat is running properly and you specified the correct port, the browser displays the Tomcat homepage.

What is the default server port number for Tomcat?

With the default configuration, Apache Tomcat will listen for requests on port 8080. To use a different port, edit the server.

How do I find the port number of Tomcat in Linux?

Once you have that PID, you can netstat -plan | grep [PID] to get your port number. Or just look in /path/to/your/tomcat/conf/server. xml for any <Connector> elements, each of which should have a port specified.


4 Answers

With this:

List<String> getEndPoints() throws MalformedObjectNameException,         NullPointerException, UnknownHostException, AttributeNotFoundException,         InstanceNotFoundException, MBeanException, ReflectionException {     MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();     QueryExp subQuery1 = Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"));     QueryExp subQuery2 = Query.anySubString(Query.attr("protocol"), Query.value("Http11"));     QueryExp query = Query.or(subQuery1, subQuery2);     Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"), query);     String hostname = InetAddress.getLocalHost().getHostName();     InetAddress[] addresses = InetAddress.getAllByName(hostname);     ArrayList<String> endPoints = new ArrayList<String>();     for (Iterator<ObjectName> i = objs.iterator(); i.hasNext();) {         ObjectName obj = i.next();         String scheme = mbs.getAttribute(obj, "scheme").toString();         String port = obj.getKeyProperty("port");         for (InetAddress addr : addresses) {             if (addr.isAnyLocalAddress() || addr.isLoopbackAddress() ||                  addr.isMulticastAddress()) {                 continue;             }             String host = addr.getHostAddress();             String ep = scheme + "://" + host + ":" + port;             endPoints.add(ep);         }     }     return endPoints; } 

You will get a List like this:

[http://192.168.1.22:8080] 
like image 56
ggrandes Avatar answered Sep 24 '22 05:09

ggrandes


For anybody who is interested in how we solved this, here is the mock code

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());                 }             }           } 
like image 45
Teja Kantamneni Avatar answered Sep 22 '22 05:09

Teja Kantamneni


public void getIpAddressAndPort() 
throws MalformedObjectNameException, NullPointerException,
            UnknownHostException {

        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();

        Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));

        String host = InetAddress.getLocalHost().getHostAddress();
        String port = objectNames.iterator().next().getKeyProperty("port");

        System.out.println("IP Address of System : "+host );
        System.out.println("port of tomcat server : "+port);

    }
like image 25
Arun Maharana Avatar answered Sep 25 '22 05:09

Arun Maharana


The server port number doesn't exist. It can have any number of port numbers. So what you're asking doesn't make sense. The port number associated with a specific request does make sense.

like image 40
user207421 Avatar answered Sep 24 '22 05:09

user207421