Java Encode and Decode URL Query String using Apache Commons Codec

Tags: Apache Commons Codec URLCodec Java Encode URL Java Decode URL

In this Java tutorial we learn how to encode a String to URL safe form and decode the URL safe string to original form using the URLCodec class of Apache Commons Codec library.

Table of contents

  1. Add Apache Commons Codec Dependency to Java project
  2. How to Encode a String to URL safe form in Java
  3. How to Decode URL safe String in Java

Add Apache Commons Codec Dependency to Java project

To use Apache Commons Codec library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'

To use the Apache Commons Codec library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>

To have more information about the Apache Commons Codec Java library you can visit the project home page at commons.apache.org/proper/commons-codec/

How to Encode a String to URL safe form in Java

The Apache Commons Codec provides URLCodec.encode() method to allow encode a String value into URL safe form.

URLCodec urlCodec = new URLCodec();
String encodedString = urlCodec.encode(inputString);

For example we have a URL http://example.com?search= and we want to combine value of search query string such as “test@string#to end:code//<>” then we need to encode to ensure the value in URL safe form by using URLCodec.encode() method to encode it as following Java program.

UrlEncodeExample1.java

import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;

public class UrlEncodeExample1 {
    public static void main(String... args) throws EncoderException {
        String url = "http://example.com?search=";
        String inputString = "test@string#to end:code//<>";

        URLCodec urlCodec = new URLCodec();
        String encodedString = urlCodec.encode(inputString);

        url = url + encodedString;
        System.out.println("Input String: " + inputString);
        System.out.println("Encoded String: " + encodedString);
        System.out.println("URL: " + url);
    }
}
The output as below.
Input String: test@string#to end:code//<>
Encoded String: test%40string%23to+end%3Acode%2F%2F%3C%3E
URL: http://example.com?search=test%40string%23to+end%3Acode%2F%2F%3C%3E

How to Decode URL safe String in Java

With Apache Commons Codec we can use the URLCodec.decode() method to decode a String in URL safe form into original String.

URLCodec urlCodec = new URLCodec();
String decodedString = urlCodec.decode(inputString);

The following Java example program to show how to use URLCodec.decode() method in detail.

UrlDecodeExample1.java

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;

public class UrlDecodeExample1 {
    public static void main(String... args) throws DecoderException {
        String inputString = "test%40string%23to+end%3Acode%2F%2F%3C%3E";

        URLCodec urlCodec = new URLCodec();
        String decodedString = urlCodec.decode(inputString);

        System.out.println("Input String: " + inputString);
        System.out.println("Decoded String: " + decodedString);
    }
}
The output as below.
Input String: test%40string%23to+end%3Acode%2F%2F%3C%3E
Decoded String: test@string#to end:code//<>

Happy Coding 😊

Java Apache Commons Codec Tutorial