By Md. Sabuj Sarker | 8/22/2017 | General |Beginners

Working With URLs in Java

Working With URLs in Java

Working with URLs in Java is fun and easy, and Java provides clean way of working with them. If you do not understand URLs well along with the usage of their different components, you should read another article I wrote titled "Understanding Universal Resource Locator (URL) for Programmers."

Getting Started With an Example

As always I like to explain things with examples.

https://en.wikipedia.org

If we parse the URL, we get HTTPS as scheme, en.wikipedia.org as host and the default port of the scheme is 443. But, we do not want to do this by hand. We want to do this with the help of Java code. A URL can be used as a string in Java but that will not be fruitful. We want to separate parts of it and use it as an Object. There is a class in Java called URL that lives inside the java.net package.

URL Parsing With the URL class

Let's create a Java project in any IDE you like and write some code similar to mine. I am going to name the public class as JavaURL—you can name it whatever you like. I am also importing the java.net package to get the URL class imported.

import java.net.*;

public class JavaURL {
   public static void main(Stings[] args){
       Srting urlString = "https://en.wikipedia.org";
       URL url = URL(urlString);
   }
}

 

Handling Exceptions Caused by Malformed URLs

Errors and exceptions are unfortunately part of a programmer's daily life. We need to handle them carefully. The code might throw a MalformedURLException. Let's enclose the code inside try..catch block.

import java.net.*;

public class JavaURL {
   public static void main(String[] args) {
       String urlString = "https://en.wikipedia.org";
       try {
           URL url = new URL(urlString);
       } catch (MalformedURLException ex){
           System.out.println("MalformedURLException occurred");
           ex.printStackTrace();
       }
   }
}

Compile the project to see if it compiles successfully. Run it to check more. It works perfect for me. Now, let's make some runtime exception by placing an invalid URL.

import java.net.*;

public class JavaURL {
   public static void main(String[] args) {
       String urlString = "https  :  //en.wikipedia   .org";
       try {
           URL url = new URL(urlString);
       } catch (MalformedURLException ex){
           System.out.println("MalformedURLException occurred");
           ex.printStackTrace();
       }
   }
}


So, there is an error message like the following. We caught the error and printed the stacktrace with an additional message.

MalformedURLException occurred
java.net.MalformedURLException: no protocol: https  :  //en.wikipedia   .org
   at java.net.URL.<init>(URL.java:586)
   at java.net.URL.<init>(URL.java:483)
   at java.net.URL.<init>(URL.java:432)
   at JavaURL.main(JavaURL.java:7)

Getting Various Components of The URL

Now, we have a URL object and we can get individual URL components easily by calling various methods on the object. Let's take an example:

import java.net.*;

public class JavaURL {
   public static void main(String[] args) {
       String urlString = "https://en.wikipedia.org";
       try {
           URL url = new URL(urlString);

           System.out.println("Scheme/Protocol of the URL: " + url.getProtocol());
           System.out.println("Host of the URL: " + url.getHost());
           System.out.println("Port of the URL: " + url.getPort());
           System.out.println("Path of the URL: " + url.getPath());
           System.out.println("Query string of the URL: " + url.getQuery());
           System.out.println("Fragment identifier of the URL: " + url.getRef());
       } catch (MalformedURLException ex){
           System.out.println("MalformedURLException occured");
           ex.printStackTrace();
       }
   }
}

After compiling and running your code you will see the following output on the screen,

Scheme/Protocol of the URL: https
Host of the URL: en.wikipedia.org
Port of the URL: -1
Path of the URL:
Query string of the URL: null
Fragment identifier of the URL: null

Our URL did not have a port set and thus we have got -1 in return. The URL's path is also empty. Query string and fragment identifier are empty and thus we have got null in return.

Let's try with a URL containing different parts to test our code. I am taking the following URL as an example.

https://en.wikipedia.org/w/index.php?search=Programmers%27+Joke&title=Special:Search&go=Go&searchToken=2rcj4uhsfa5n6eh6jfblmxg6j#frag-id

Compile and run the code with the above URL to see the following result.

Scheme/Protocol of the URL: https
Host of the URL: en.wikipedia.org
Port of the URL: -1
Path of the URL: /w/index.php
Query string of the URL: search=Programmers%27+Joke&title=Special:Search&go=Go&searchToken=2rcj4uhsfa5n6eh6jfblmxg6j
Fragment identifier of the URL: frag-id

The port is still missing. Let's put a port number inside the URL:

https://en.wikipedia.org:8080/w/index.php?search=Programmers%27+Joke&title=Special:Search&go=Go&searchToken=2rcj4uhsfa5n6eh6jfblmxg6j#frag-id

Hit build and run the program to see the following result.

Scheme/Protocol of the URL: https
Host of the URL: en.wikipedia.org
Port of the URL: 8080
Path of the URL: /w/index.php
Query string of the URL: search=Programmers%27+Joke&title=Special:Search&go=Go&searchToken=2rcj4uhsfa5n6eh6jfblmxg6j
Fragment identifier of the URL: frag-id

This time we have got everything we want. Now, it's time to discuss further the URL class and its various methods.

Different Constructors of the URL class

The URL class from the java.net package has different constructors constructing an Object. Remember that all the constructors throw MalformedURLException exception.

  • URL(String url)

We already have used this constructor to show all the examples above. This constructor does not need any component as a separate argument.

  • URL(String protocol, String host, int port, String file)

This constructor needs four arguments to be passed during the object creation. You can put protocol, host, port number and file separately. You may see a new thing called file here. It is a combination of path, query string and the fragment identifier.

  • URL(String protocol, String host, String file)

With this constructor you do not need to provide the port number. During working with this a default port number for the protocol will be used. For example the default port number for HTTP is 80.

  • URL(URL context, String url)

This a very different type of constructor compared to others. It accepts a URL object as a context. Any URL passed as a string will be resolved with the reference to the context URL. This is handy when you have a relative URL and want to get a full URL with the help of the context URL object.

Methods for Getting the Defaults

In our example, without the port being set, we see that we get -1 when we call getPort() method on URL object. But, every protocol has a default port number. getPort() should have provided us with the default port number when it is not set. But Java believes in separation. We have another method for getting the default port that is getDefaultPort(). Look at the example.

String urlString = "https://en.wikipedia.org/w/index.php?search=Programmers%27+Joke&title=Special:Search&go=Go&searchToken=2rcj4uhsfa5n6eh6jfblmxg6j#frag-id";
try {
   URL url = new URL(urlString);
   System.out.println("Default Port of the URL: " + url.getDefaultPort());
} catch (MalformedURLException ex){
   System.out.println("MalformedURLException occured");
   ex.printStackTrace();
}

The output:

Default Port of the URL: 443

Perfect! As the default port of HTTPS is 443, we have got the same as output. Remember that the method will return the same default port regardless of whether a port is set with the URL or not. To experiment with this you use the urlString as follows:-

String urlString = "https://en.wikipedia.org:8080/w/index.php?search=Programmers%27+Joke&title=Special:Search&go=Go&searchToken=2rcj4uhsfa5n6eh6jfblmxg6j#frag-id";

And the result is still 443 in spite of being the port is set as 8080.

Important Methods of URL class

There are many methods of URL classes, but I am not going to cover them all. I am going to cover the ones that are most important. To know about other methods you can take a look at the official Java documentation.

  • getProtocol()

Formally we call it the scheme but protocol is also not an inaccurate term. Every protocol has an associated port number by default. It returns a string.

  • getHost()

Returns host as a string. The host can be an IP address or an internet domain name.

  • getPort()

getPort() does not return the result as a string. It returns an integer. When the port is not present it might return -1.

  • getPath()

getPath() returns the path component of the URL as a string. Path is an optional part of an URL and it might not be present. So, program your code such that it can go without path.

  • getQuery()

getQuery() returns the query string. Query in URL contains one or more pairs of key value where the value is optional. Query string is also optional in a URL and thus might not always be present.

  • getRef()

The fragment identifier of the parsed URL is retrieved by the getRef() method as a string.

I haven't provide much information on the methods discussed above. I already have discussed the concept of these in another article mentioned at the beginning of this article. I encourage you to read that article thoroughly and I also encourage you to read the official documentation on the URL class.

*Stop by the homepage to search and compare SDKs, Dev Tools, and Libraries.

By Md. Sabuj Sarker | 8/22/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now