2

I have a web application. Whenever it is deployed, a servlet is started. As soon as servlet starts it must register itself as a callback url to an external service to get back some notifications. Registering a callback could be just a simple POST request to external service with callback url in the request body. I searched a lot but could not find any way on how to construct the callback url in the servlet.
e.g. if application is deployed as protocol://A.B.C.D:XXXX and servlet path is /myservlet then callback url would become protocol://A.B.C.D:XXXX/myservlet
I am not sure how to get the IPaddress (A.B.C.D) and port number (XXXX) in the servlet code. Can anyone help here ?
Note that it should be done as soon as servlet starts, probably in init() hence I do not have HTTPServletRequest object to get this info.

snow_leopard
  • 1,466
  • 2
  • 20
  • 36
  • 3
    A Servlet cannot generally (and probably should not) know what host machine it's running on. IMO, you'll need some external configuration. – Sotirios Delimanolis Oct 02 '13 at 13:42
  • 1
    Without request, how can you get that URL of that request ? As Sotirios said, no way. – Suresh Atta Oct 02 '13 at 13:45
  • Notice, that if you want to code to be run as soon the container start you shoul use a ServletContextListner, not the init method of a servlet. That init only would be called if the servlet is instanciated. More info http://stackoverflow.com/questions/13819773/how-to-run-specific-java-code-on-tomcat-start-or-on-application-deploy – polypiel Oct 02 '13 at 13:55
  • Even then how will I get the machine address and port where application is deployed ? – snow_leopard Oct 02 '13 at 14:03

3 Answers3

1

The information you are looking for isn't available through the Servlet API unless a request is in progress. Individual containers may have container specific APIs that enable you to do this but what ever you produce will not be portable.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
-1

To get server ip, you can use this:

public String getServerIpAdd()
    {
        String ipAddr = "";
        try 
        {
            InetAddress inetAddr = InetAddress.getLocalHost();
            byte[] addr = inetAddr.getAddress();
            // Convert to dot representation           
            for (int i = 0; i < addr.length; i++) {
                if (i > 0) {
                    ipAddr += ".";
                }
                ipAddr += addr[i] & 0xFF;
            }            
        } catch (UnknownHostException e) {
            System.out.println("Host not found: " + e.getMessage());
        }
        return ipAddr;
    }

To get servlet name, you can use getServletName() on your servlet.

Based on this topic, you can do this to get the server port:

public void doGet(ServletRequest request, ServletResponse response) {
    System.out.println("Host Name = " + request.getServerName());
    System.out.println("Port Number = " + request.getServerPort());
}
Community
  • 1
  • 1
Cold
  • 787
  • 8
  • 23
-1

I believe the servlet url mapping is always fixed! since you've already posted that in web.xml

In case it's not fixed, you can read web.xml from the classpath and get it.

[Here's how to read XMLs] in java

And there after you can use @ColdHack's answer to get the server IP.

Now you have the IP and the servlet name, you can construct the URL and POST it to the external service.

You can do all this in the context-listener, that way everytime it starts, it registers itself.

coding_idiot
  • 13,526
  • 10
  • 65
  • 116