foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment
The URL class in Java is too stringent to accept our use cases here, namely you can not create a URL class that excludes the scheme and authority portions of a URL. However, Java does include a URI class which is suitable for our needs. The URLBuilder class doesn't help you build the scheme or authority parts of a URL. The URI class in java.net handles that part perfectly well. This class helps you with the path, query, and fragment parts of a URL.
Since this class relies on the URI class to handle the scheme and authority parts of a URI, we frequently refer to them together. We've chosen the term base
. It's short and descriptive.
Here's a few examples of using URLBuilder to construct URL's. Here's an easy one:
Here's one with a query string: URI base = new URI("http://www.example.com"); URLBuilder urlb = new URLBuilder(base); urlb.add("yellow").add("brick"); urlb.add("road.html"); assertEquals("http://www.example.com/yellow/brick/road.html",urlb.toString());
All of the {@link #add(Object)} and {@link #addQuery(Object,Object)} methodsdo their thing, but also return URI base = new URI("http://www.example.com"); URLBuilder urlb = new URLBuilder(base); urlb.add("people.xml").addQuery("title","manager"); assertEquals("http://www.example.com/people.xml?title=manager",urlb.toString());
this
. Which means you can chain the methods together like we have done in both of our examples so far. We don't have to have a base, we can create just the path component and the query string:.
@version $LastChangedRevision$ URLBuilder urlb = new URLBuilder(); urlb.add("people.xml") urlb.addQuery("title","manager"); assertEquals("people.xml?title=manager",urlb.toString());
$LastChangedDate$
@author $LastChangedBy$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|