Skip to content
Related Articles
Open in App
Not now

Related Articles class in Java

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 16 Jun, 2017
Improve Article
Save Article

This class is a utility class for HTML form encoding. Encoding makes the form of URL more reliable and secure. When the user request is triggered by a get method, the form parameters and their values are appended at the end of URL after a ‘?’ sign. The problem arises when special characters are used for their values. In general case, HTML handles the encoding part and automatically processes the special characters and convert them to special characters for smooth handling of all the operations. However it is not a good practice to rely solely on HTML features and thus java provides this class to explicitly encode the URLs.
Following rules are used when encoding a string:

  1. Alphanumeric characters and certain special characters such as ‘*‘, ‘_‘, ‘‘ and ‘.‘ remains unchanged.
  2. Spaces are converted into ‘+‘ signs.
  3. All other characters are encoded by one or more bytes using the encoding scheme specified. They are converted in a three character string of the form %xy, where xy represents the hexadecimal representation of the encoding character. W3C recommends using “UTF-8” for encoding purposes.

For example, if we have the parameter value which contains special characters and spaces as

u@geeks for geeks

If the encoding used is UTF-8 which is most common used, the @ sign will be converted into %40 and spaces would be converted to + signs and our encoded string will look like-


Methods :

  1. encode() : This is one and only method provided by this class. It as the name suggests returns an encoded string for the specified string. One method, which is now deprecated has only one parameter, the string to be encoded. It doesn’t let you specify the encoding to be used and uses the platform default encoding. Another version allows the specification of the encoding to be used, and thus is widely used.
    Syntax :public static String encode(String s) - @Deprecated
    Parameters :
    s : String to be encoded
    Syntax :public static String encode(String s,
                String enc)
                         throws UnsupportedEncodingException
    Parameters : 
    s : string to be encoded
    enc : encoding to be used
    Throws :
    UnsupportedEncodingException : If the specified encoding is not used

Java Implementation :

// Java program to show encode() method of 
// URLEncoder class
public class UrlEncoder 
    public static void main(String[] args) throws MalformedURLException, 
        // base URL
        String baseurl = "";
        // String to be encoded
        String query = "u@geeks for geeks";
        System.out.println("URL without encoding :");
        URL url = new URL(baseurl + query);
        // encode() method
        System.out.println("URL after encoding :");
        url = new URL(baseurl + URLEncoder.encode(query, "UTF-8"));

Output :

URL without encoding : for geeks
URL after encoding :

References :
Official Java Documentation
This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!