tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier>
Subject Re: getServerName returns 'localhost'
Date Wed, 03 Mar 2010 14:29:08 GMT
Peter Crowther wrote:
> On 3 March 2010 13:47, André Warnier <> wrote:
>> André Warnier wrote:
>>> vgud wrote:
>>>> My server responds to different domain names, and I want to know which
>>>> one users use.
> [...]
>> More precisely :
>> getRemoteAddr
>> public java.lang.String getRemoteAddr()
>>    Returns the Internet Protocol (IP) address of the client or last proxy
>> that sent the request. For HTTP servlets, same as the value of the CGI
>> variable REMOTE_ADDR.
> Andre, you're answering the question of "which client sent this request?"
> The OP is asking a different question: "What hostname did the client
> send this request to?"
> I'd parse the host header in the request to find out, but that's just me!

You are right, I am off-base here, apologies.

String hostname = request.getHeader("Host") should work.

But then, the question is, according to the OP, request.getServerName() 
returns "localhost", although that is very unlikely to have been used by 
the client in the Host header.
However, the java API says :


public java.lang.String getServerName()

     Returns the host name of the server to which the request was sent. 
It is the value of the part before ":" in the Host header value, if any, 
or the resolved server name, or the server IP address.

         a String containing the name of the server

That does not seem to work in his case.

Does it only work when the <Host> or <Alias> tags with the corresponding 
names are set ?
(A question for people who like to scan the code..  Christopher ?)

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message