Java Exploration in ColdFusion: java.net.URL

Posted September 28, 2006 at 9:29 AM by Ben Nadel

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

Nov 5, 2008 at 11:26 AM // reply »
5 Comments

Hey Ben....
Thanks for this. Saved me a lot of coding time. Keep up the good work.

See you at CFUnited 09
Yaron


Nov 5, 2008 at 11:28 AM // reply »
10,640 Comments

@Yaron,

Glad to help out. How's Florida treating you?


Nov 5, 2008 at 12:37 PM // reply »
5 Comments

Beautiful...Low 80's & Sunny...But I'll be visiting NY next month...Not looking forward to a bone chill.


Nov 5, 2008 at 12:44 PM // reply »
10,640 Comments

Damn you :) If you wanna grab lunch or anything when you're here, feel free to hit me up.


Aug 18, 2011 at 10:43 AM // reply »
1 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>


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »