URI String and Components in Java

This section provides a tutorial example on how to create URI strings with URI components using Java java.net.URI class.

To do URI encoding in Java language, we need to use the java.net.URI class, which offers several interesting constructors and methods:

URI(String scheme, String authority, String path, String query, String fragment) - Constructor creating a hierarchical URI from given components. The string provided for each component will be URI encoded with some exceptions specific for that component.

toString() - Method returns the encoded string representation of this URI.

toASCIIString() - Method returns the fully encoded string representation of this URI.

get<component>() - Method returns the decoded string representation of a URI component. For example, getPath(), getQuery(), etc.

getRaw<component>() - Method returns the encoded string representation of a URI component. For example, getRawPath(), getRawQuery(), etc.

To test the URI class, I wrote this tutorial example program:

/* UriEncodingTest.java
 * Copyright (c) 2010 HerongYang.com. All Rights Reserved.
 */
import java.net.URI;
class UriEncodingTest {
   public static void main(String[] a) {

      URI uri = null;
      String out = null;

      String unicode = "\u548c";
      String space = " ";
      String reserved = "!*'();:@&=+$,/?#[]";
      String input = unicode+space+reserved;

// URI example
      String scheme = "http";
      String authority = "herongyang.com";
      String path = "/encoding/index.html";
      String query = "c=eu&l=en";
      String fragment = "toc";
      try {
         uri = new URI(scheme, authority, path, query, fragment);
         out = "\n";
         out += "URI example:\n";
         out += "        URI string: "+uri.toString()+"\n";
         System.out.print(out);
      } catch (Exception e) {
         e.printStackTrace();
      }

// URI components
      scheme = "scheme";
      authority = "authority";
      path = "/path";
      query = "query";
      fragment = "fragment";
      try {
         uri = new URI(scheme, authority, path, query, fragment);
         out = "\n";
         out += "URI components:\n";
         out += "        URI string: "+uri.toString()+"\n";
         System.out.print(out);
      } catch (Exception e) {
         e.printStackTrace();
      }

// URI path test
      try {
         uri = new URI(scheme, authority, "/"+input, query, fragment);
         out = "\n";
         out += "path test:\n";
         out += "      Encoded path: "+uri.getRawPath()+"\n";
         out += "      Decoded path: "+uri.getPath()+"\n";
         out += "        URI string: "+uri.toString()+"\n";
         out += "  URI ASCII String: "+uri.toASCIIString()+"\n";
         System.out.print(out);
      } catch (Exception e) {
         e.printStackTrace();
      }

// URI query test
      try {
         uri = new URI(scheme, authority, path, input, fragment);
         out = "\n";
         out += "query test:\n";
         out += "     Encoded query: "+uri.getRawQuery()+"\n";
         out += "     Decoded query: "+uri.getQuery()+"\n";
         out += "        toString(): "+uri.toString()+"\n";
         out += "   toASCIIString(): "+uri.toASCIIString()+"\n";
         System.out.print(out);
      } catch (Exception e) {
         e.printStackTrace();
      }

// URI fragment test
      try {
         uri = new URI(scheme, authority, path, query, input);
         out = "\n";
         out += "fragment test:\n";
         out += "  Encoded fragment: "+uri.getRawFragment()+"\n";
         out += "  Decoded fragment: "+uri.getFragment()+"\n";
         out += "        toString(): "+uri.toString()+"\n";
         out += "   toASCIIString(): "+uri.toASCIIString()+"\n";
         System.out.print(out);
      } catch (Exception e) {
         e.printStackTrace();
      }

      return;
   }
}

If you run this program in JDK 1.6, you should get:

URI example:
        URI string: http://herongyang.com/encoding/index.html
                    ?c=eu&l=en#toc

URI components:
        URI string: scheme://authority/path?query#fragment

path test:
      Encoded path: /?%20!*'();:@&=+$,/%3F%23%5B%5D
      Decoded path: /? !*'();:@&=+$,/?#[]
        URI string: scheme://authority
                    /?%20!*'();:@&=+$,/%3F%23%5B%5D
                    ?query#fragment
  URI ASCII String: scheme://authority
                    /%E5%92%8C%20!*'();:@&=+$,/%3F%23%5B%5D
                    ?query#fragment

query test:
     Encoded query: ?%20!*'();:@&=+$,/?%23[]
     Decoded query: ? !*'();:@&=+$,/?#[]
        toString(): scheme://authority/path
                    ??%20!*'();:@&=+$,/?%23[]#fragment
   toASCIIString(): scheme://authority/path
                    ?%E5%92%8C%20!*'();:@&=+$,/?%23[]#fragment

fragment test:
  Encoded fragment: ?%20!*'();:@&=+$,/?%23[]
  Decoded fragment: ? !*'();:@&=+$,/?#[]
        toString(): scheme://authority/path?query
                    #?%20!*'();:@&=+$,/?%23[]
   toASCIIString(): scheme://authority/path?query
                    #%E5%92%8C%20!*'();:@&=+$,/?%23[]

Conclusion:

Table of Contents

 About This Book

 Base64 Encoding

 Base64 Encoding and Decoding Tools

 Base64URL - URL Safe Base64 Encoding

 Base32 Encoding

URL Encoding, URI Encoding, or Percent Encoding

 What Is URL/URI Encoding?

 URL Encoding Variations

 URL Encoding on HTML Form Data - IE

 URL Encoding on HTML Form Data - Firefox

 application/x-www-form-urlencoded Encoding in Java

URI String and Components in Java

 URL Encoding Support in PHP

 UUEncode Encoding

 References

 Full Version in PDF/EPUB