3 Replies Latest reply on Jul 20, 2009 6:53 PM by Alex Cougarman

    Getting client hostname

    Alex Cougarman Apprentice

      We're trying to get the client hostname/computer name without much luck in our Seam app. Here's the code:


      public String getHostName() throws UnknownHostException{
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        return "Remote Addr: " + request.getRemoteAddr() + 
          "; Remote Host: " + request.getRemoteHost() +
          "; Remote User: " + request.getRemoteUser() +
          "; Localname: " + request.getLocalName() +
          "; local hostname: " + java.net.InetAddress.getLocalHost().getHostName();
      }



      This returns the following:


      Remote Addr: 192.168.25.196; Remote Host: 192.168.25.196; Remote User: null; Localname: webserv-dev1.unctv.org; local hostname: dev1
      



      We've toggled the enableLookup on Apache but something is still blocking the right info from reaching us. Or maybe we're not using the right code. Thanks.

        • 1. Re: Getting client hostname
          Nicklas Karlsson Master

          Well, you are in the 192.168.* net so perhaps it doesn't have a name that can be looked up?

          • 2. Re: Getting client hostname
            Jean Luc Apprentice

            The are many problems with that code.


            At the most fundamental level, getLocalName() returns the name of the server interface through which the request was received. local is local to the server. From the server, the browser is the remote user.


            Even then (if you tried to do the same with the remote host), you're asking the DNS servers available to the server to resolve an IP that's defined on the client. There are multiple problems with this: if private ranges are used, they are not unique at all. And in this case the client does use one. Then the OS matters, as well as whether there are multiple cards, each with its own host name. But all this is moot because the server (the HttpServletRequest object is on the server side) does not know the host name  of the machine on which the HTTP request originates. Packets that reach the server may have been passed through proxies and so on. The remote host seen by the server is not necessarily the origin. The network layer thus cannot tell you that information and the HTTP protocol does not send the local machine name (for a number of reasons, security included).


            For security reasons, Javascript does not allow the code to read it either.


            AFAIK, there are no portable solutions for what you want.


            • 3. Re: Getting client hostname
              Alex Cougarman Apprentice

              Thank you, Nicklas and Jean. Very useful information and it's good to know that I wasn't losing my mind -- that the task at hand is impossible. I'll stick with IP address.