Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with:

Java Exploration in ColdFusion: java.net.URL

By Ben Nadel on
Tags: ColdFusion

I was looking at Dan G. Switzer, II's post about parsing a URI when it got me thinking: Java has so much awesome stuff, what do they have for URLs. It turns out, there is a java.net.URL class that is all about parsing out URL information. I took it for a spin.

First, I created a URL:

  • <!--- Set the URL. --->
  • <cfset strUrl = (
  • "http://ben:nadel@www.college-cuties.com:80/" &
  • "freshman-girls/brunettes.cfm" &
  • "?single=true&min_hotness=7" &
  • "##getstarted"
  • ) />

I tried to put as much info into the URL as I could to see how it all got parsed out. Then, I created a URL object and initialized it with the given Url:

  • <!--- Create a Java URL. --->
  • <cfset jUrl = CreateObject( "java", "java.net.URL" ).Init(
  • strUrl
  • ) />

The java.net.URL has a bunch of classes that can all be found in the Java 2 documentation. Here is what each of them does when called on the previously created jUrl object:

jUrl.getAuthority()

ben:nadel@www.college-cuties.com:80

jUrl.getContent()

sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d4267d

jUrl.getDefaultPort()

80

jUrl.getFile()

/freshman-girls/brunettes.cfm?single=true&min_hotness=7

jUrl.getHost()

www.college-cuties.com

jUrl.getPath()

/freshman-girls/brunettes.cfm

jUrl.getPort()

80

jUrl.getProtocol()

http

jUrl.getQuery()

single=true&min_hotness=7

jUrl.getRef()

getstarted

jUrl.getUserInfo()

ben:nadel

jUrl.toExternalForm()

http://ben:nadel@www.college-cuties.com:80/freshman-girls/ brunettes.cfm?single=true&min_hotness=7#getstarted

That's some pretty cool stuff. I guess most of this is available via the CGI variables, but it's nice to know you can create objects. You can all get the content of the target page of the given URL in this method, but I haven't had time to play with that yet.



Reader Comments

Those Java methods realy saves time! I had to convert punycode to IDN. It was very simple when I found the right class (borowed a bit from your code..):

  • <cfset strUrl = "xn--land-poa.se" />
  •  
  • <!--- Create a Java URL. --->
  • <cfset jUrl = CreateObject( "java", "java.net.IDN" ).toUnicode(strUrl) />
  •  
  • <cfoutput>
  • #jUrl#
  • </cfoutput>