• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to obtain hostname during servlet init

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to obtain the host name of the server my web app is running on during the init() method of one of my servlets (without resorting to init params or system properties which could speak with false tongue).
Any clues? There doesn't seem to be any way to get this info from the servlet config or servlet context.
You can get this from a request (using getServerName()) but the init() method is, of course, called long before any requests are made.
Seems like this is certainly something that should be able to be determined, but I can't seem to find a way.
Any clues would be most appreciated!
thanks,
bear
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is that a host doesn't necessarily know its name - especially if its behind a firewall or router or load balancer or edge caching technology or whatever.
It can know its own name, but that name may have nothing to do with how a client browser would address it.
For example, I can do a ping of www.yahoo.com and get something like this (some stuff deleted):
$ ping www.yahoo.com
PING www.yahoo.akadns.net (66.218.71.81) ...
64 bytes from w2.scd.yahoo.com (66.218.71.81): ...
64 bytes from w2.scd.yahoo.com (66.218.71.81): ...
Now if you were coding the yahoo server, which host name would you want: www.yahoo.com, www.yahoo.akadns.net, or w2.scd.yahoo.com or any of the who knows how many others that are out there serving yahoo content?
And maybe you have one server hosting requests from several different names.
So maybe lazily initialize it off the first request. Hopefully that request is not going to be an admin testing with http://localhost/ So maybe its even better to query each request for "how did you get here" information.
Anyway, back to your question, if you still want the host name, how about
java.net.InetAddress.getLocalHost().getHostName()
or .getCanonicalHostName() (JDK 1.4).
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dave,
Thanks for the info. I decided to go with the lazy initialization scheme. Probably better for startup performance, anyways.
thanks again!
bear
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic