Getting the server hostname programmatically

December 02, 2003
coldfusionjava

Need to know a server's hostname, not the http server name. Try this:
<cfset inet = CreateObject("java", "java.net.InetAddress")>
<cfset inet = inet.getLocalHost()>
<cfoutput>#inet.getHostName()#</cfoutput>

7 people found this page useful, what do you think?

Comments

why not just use #cgi.server_name#?
That returns different values depending on the web site domain. This method always returns the server's hostname. I'm not sure why you would need that specifically, but someone asked for it, and this is the solution I came up with.
Thanks for the tip Pete. An alternative we used was to read the name from the registry: <cfregistry action="get" branch="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" entry="ComputerName" variable="computerName" type="string" />. Of course this isn't an option if you use *nix... We use the hostname in our applications to <cfinclude> machine-specific settings. This enables us to maintain the same codebase on our development boxes that we have in our test and production environments.
Does anyone know of any documentation out there, on Macromedia or elsewhere, of all the Java variables and functions available to you like this? How about "factory services"?
Hi Jason, What I'm using here is part of the standard Java API. Documentation for it can be found here: http://java.sun.com/j2se/1.4.1/docs/api/ The service factories in ColdFusion MX are not documented by Macromedia at all, they don't really want you messing with them. When CFMX first came out I posted some undocumentation here: http://www.cfdev.com/mx/undocumentation/ it doesn't cover the service factory, but it does cover some other features.
"documentation out there on Macromedia Java variables and functions" its called fusdocs (Coldfusion MX javadocs)
what is the server hostname for tripod/lycos. i need this info to get my website up which is hosted by tripod and the domain name is sold to me by funcity.com thanks ruel brown
This is quite useful, if you are running behind a cluster or load-balancing configuration and would like to log or write (hidden, in the HTML response) the id of the server which responded to the request.
Thanks Pete. I needed to know how to do that because we have multiple web servers and they each need to write to a log named after their actual computer name to a shared folder. There were locking problems when they all tried to write to the same file, as you would expect. (I know it's another old blog entry I'm replying to but I wanted to register a thank you!)
I'm out of leauge here. Too much brain power on display!

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?