Java Reference
In-Depth Information
URL url2 = new URL(url1, "search/node/jdk8");
As you can see, the path
search/node/jdk8
is relative to the URL that is
known as
url1
. In the end, the human-readable format of the
url2
object is represen-
constructors for creating URL objects that take more than two arguments. Those con-
structors are as follows:
new URL (String protocol, String host, String port,
String path);
new URL (String protocol, String host, String path);
In the solution, the second of the two constructors shown here is demonstrated. The
protocol, hostname, and path of the resource are passed to the constructor to create the
url3
object. These last two constructors are usually most useful when you're dynam-
ically generating an URL.
21-6. Parsing a URL
Problem
You want to programmatically gather information from an URL in your application.
Solution
Parse the URL using the built-in URL class methods. In the following example class
named
ParseUrl
, an URL object is created and then parsed using the built-in URL
class methods to gather information regarding the URL. After the information has been
retrieved from the URL, it is printed to the command line and then used to create an-
other URL.
import java.net.MalformedURLException;
import java.net.URL;
public static void main(String[] args) {
URL url1 = null;