11

My application is an Spring framework 3.1.2 based Web-application deployed on Apache tomcat 6.

I need to get my running application port-number and host-name on application(server) Start-up. So that I would override it on a property and it is need for Other bean initialization.

Does the spring provides any options to retrieves these details and to set it on Server Startup?..

omega
  • 592
  • 2
  • 5
  • 21

4 Answers4

6

Take a look into those two questions previously asked: Get the server port number from tomcat with out a request and I need to know the HTTP and HTTPS port my java webapp is running on webapp startup. There you will see how to get port from a Connector, connector also has getDomain method that would give you host name.

Since you know how to get without spring, you can have a bean that gets those details and provides them for the other bean that needs those instantiation detail. There are few ways to do that:

1) Create Spring factory bean that would get port, hostname and instantiates bean you want

2) Have separate bean that holds those details for you and you use that bean to construct other one

3) You override your application details with port and domain and when instantiating bean that needs them have a init method that would read them for your new bean

Community
  • 1
  • 1
Eds
  • 781
  • 1
  • 5
  • 7
5

Here is code for getting port an dip address

class IPAddressDemo{
    public static String getIpAddressAndPort1() 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");
        String ipadd = "http" + "://" + host + ":" + port;
        System.out.println(ipadd);
        return ipadd;
    }
}
Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
Arun Maharana
  • 291
  • 5
  • 3
  • That's ideas solution if Proxy is replacing real Connector's port number (configured in server.xml) and you need to get it. Thanks a lot!! – Renat Gatin Mar 06 '19 at 21:17
-3

Tomcat will run by default on TCP/IP port number 8080.

Steps

  1. Navigate to C:\apache-tomcat-6.0.18\conf\server.xml (The place where you have installed tomcat)
  2. In server.xml file, find Connector port looking like the following

    <connector port="8080" protocol="HTTP/1.1" connectiontimeout="20000" redirectport="8443">     
    
    </connector>
    

port in the Connector tag is your port no.

Finding hostnames:

Steps

1. Navigate to `C:\WINDOWS\system32\drivers\etc` 

Or

start->All Programs->Run-> type 'drivers' (Without quotes)->etc

  1. Open the file host with a text editor and you can find
127.0.0.1       localhost

From this you can understand what your hostname is.

Thanks.

Harish Raj
  • 1,565
  • 3
  • 17
  • 27
  • 2
    I want these details(portNumber & HostName) at run-time (i.e Programmatically). Not at manual check!. – omega Dec 18 '12 at 09:41
-4

The ServletRequest object that has been passed to your doGet, or doPost method has getServerName() and getServerPort() methods that provide this information.

Example:

public void doGet(ServletRequest request, ServletResponse response) {
    System.out.println("Host Name = " + request.getServerName());
    System.out.println("Port Number = " + request.getServerPort());
}
Harish Raj
  • 1,565
  • 3
  • 17
  • 27