There is no problem getting the host name of the mcahine that the Servlet is
running on just call:
java.net.InetAddress().getLocalHost().getHostname();
Course this isn't going to solve your problem if you the machine you are
running on has more than one IP address (I believe it will always return the
first IP address) or if it is set up with virtual hosts.
As for getting the port number I can't think of any API that would give you
that info.
Why not just use a property file that your servlet read on startup that
contained the info you require?
Chris Gow
[EMAIL PROTECTED]
> -----Original Message-----
> From: pascal jacob [SMTP:[EMAIL PROTECTED]]
> Sent: Friday, June 11, 1999 1:26 PM
> To: [EMAIL PROTECTED]
> Subject: Re: How to get Host name and port number
>
> There is no problem to obtain the host and port # while in the
> doPost()
> or doGet() method - I just use request.getServerName() and
> Request.getServerPort().
> My problem is that I need these infos while in the init() method,
> before
> any doGet() or doPost() is called.
> Any idea of how to have the host name and port # while still in the
> init() method??
>
> ZartC++
>
> > ZartC
> >
> > Bear in mind that "the init() method is called only once after a
> > servlet is loaded, and it is guaranteed to finish before any requests
> > are made to the servlet".
> >
> > This is quoted from the new Karl Moss Java Servlets book. You
> can
> > certainly get the info you want in the doPost() method, which, in the
> > flow of a servlet, immediately follows the init() invocation.
> >
> > You may already be familiar with this, but just in case - in the
> > doPost() method (HttpServletRequest req)), req.getRemoteHost() should
> > get you the host name. I don't know about the port number, but maybe
> > req.getRequestURI() will show the port # in the contents returned...
> >
> > Hope this helps, Mike Drummond
> >
> >
> >
> > ----------
> > From: ZartC
> > To: SERVLET-INTEREST
> > Subject: How to get Host name and port number
> > Date: Friday, June 11, 1999 6:57AM
> >
> > My problem is to get the host name and the port number at init() time -
> i.e.
> > we don't have a ServletRequest yet !
> >
> > ZartC++
>
> __________________________________________________________________________
> _
> 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
___________________________________________________________________________
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