Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

How can I get the serverName in a servlet?

Thread view: 
argszero - 12 Nov 2007 05:08 GMT
Hi
 I use "String ip =request.getLocalAddr();". When the servlet was
request using url "http://localhost/servlet/XXXServlet" or "http://
127.0.0.1/servlet/XXXServlet",I can only get "127.0.0.1" which is not
the server public IP.How can get the public IP?
Thanks
Arne Vajhøj - 13 Nov 2007 01:23 GMT
>   I use "String ip =request.getLocalAddr();". When the servlet was
> request using url "http://localhost/servlet/XXXServlet" or "http://
> 127.0.0.1/servlet/XXXServlet",I can only get "127.0.0.1" which is not
> the server public IP.How can get the public IP?

I believe that request.getLocalAddr() returns the IP address
use to connect with.

So if you connect to localhost you get localhost and if you connect to
the real host you get the real host.

This seems rather logical to me since the host may have no real
IP address or 10 real IP addresses

Assuming you have a newer Java version you can get all IP
addresses with:

        Enumeration<NetworkInterface> e =
NetworkInterface.getNetworkInterfaces();
        while(e.hasMoreElements()) {
            NetworkInterface ni = e.nextElement();
            Enumeration<InetAddress> e2 = ni.getInetAddresses();
            while (e2.hasMoreElements()){
                InetAddress ip = e2.nextElement();
                // do something with ip.getHostAddress()
            }
        }

Arne
argszero - 13 Nov 2007 11:11 GMT
On 11 13 ,   9 23 , Arne Vajh?j <a...@vajhoej.dk> wrote:
> >   I use "String ip =request.getLocalAddr();". When the servlet was
> > request using url "http://localhost/servlet/XXXServlet" or "http://
[quoted text clipped - 25 lines]
>
> Arne

Thanks ,your solution works well when there is only one network card.
But if the server be reuested has multiple network card,how can I know
from which card did the client request?
Arne Vajhøj - 14 Nov 2007 02:42 GMT
>> Assuming you have a newer Java version you can get all IP
>> addresses with:
[quoted text clipped - 13 lines]
> But if the server be reuested has multiple network card,how can I know
> from which card did the client request?

The outer loop handles multiple cards.

ni.getName() contains the name of the card.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.