On Thu, 16 Sep 1999, Craig R. McClanahan wrote:

> Milt Epstein wrote:
[ ... ]
> > CGI apparently provides the
> > environment variables HTTP_HOST and SERVER_NAME to distinguish between
> > these (HTTP_HOST actually includes the port, while SERVER_NAME does
> > not).  I could only find getServerName() in the JSDK API.  This
> > apparently corresponds to HTTP_HOST (without the port).
>
> The ServletRequest.getServerName() method actually corresponds to
> SERVER_NAME ... the name of the virtual host that this request asked
> for.  You can get the port number with getServerPort() if you need
> it, such as to reconstruct an absolute URL.  The servlet API itself
> does not have a call for the canonical host name.
>
>
> >  Is there any
> > way I can get both of these names in a servlet?
> >
> > I thought of InetAddress.getLocalHost(), but I don't think that will
> > work.  I'm guessing that will return the canonical name of the server,
> > which is not necessarily the same.
>
> The value returned by InetAddress.getLocalHost().getHostName()
> should return the canonical host name that would correspond to the
> HTTP_HOST variable minus the port number, which will be the same for
> both.  As above, ServletRequest.getServerName() returns the name
> that matches SERVER_NAME.

Hmmm, some of this disagrees with the behavior I'm seeing.  But
perhaps I should step back and describe the situation in more detail
so as to be sure I'm being clear.

I have a machine that's known as both, say, x.y.z.edu, and w.y.z.edu
(the former is the canonical name and the latter is an alias/CNAME,
but I don't think that matters here).  I set up a web server on this
machine, particularly an https instance of NES, and in the config file
indicate that the server name is w.y.z.edu.

OK, now I pull up a browser on another machine, and call a CGI script
via the URL:

   https://x.y.z.edu/...

HTTP_HOST shows up as x.y.z.edu and SERVER_NAME as w.y.z.edu.  That
is, HTTP_HOST is the machine name specified in the URL, and
SERVER_NAME is the machine named specified in the NES config file.

Now, I call a servlet with a similar URL as above.  I get the URL
using HttpUtils.getRequestURL() (which uses getServerName()).  This
shows up as x.y.z.edu, just like HTTP_HOST.

Now, if I instead call things using URLs of the form:

   https://w.y.z.edu/...

all of the above show up as w.y.z.edu.

>From this it looks like:

   getServerName() == HTTP_HOST == name specified in the URL
   SERVER_NAME == name specifed in web server config file

I do realize that the JSDK API docs (at least for JSDK 2.0), as well
as Jason Hunter's book, indicates that getServerName() is the same as
SERVER_NAME, but that is apparently not the behavior I'm getting.

I should say I'm using a pre-release version of ServletExec 2.1 as my
servlet engine.  I haven't tried it with any other servlet engines.

Now, I haven't tried it, but I believe InetAddress.getLocalHost() will
give me the canonical name, which is really irrelevant here.  What I
want are the name used in configuring the web server, and the name
used in the URL.

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to