Javatpoint Logo
Javatpoint Logo

Java URL Encoder

Java URLEncoder is a utility class used to encode the URLs (Uniform Resource Locator). Reliability and security are ensured using Encoding of URL. When the user requests a particular site through the get method, the form parameters and their values are added after the '?' sign in the URL. However, the problem arises when special characters are used in the values that aren't interpreted. Generally, HTML handles the encoding part automatically by processing the special characters and convert them to the characters that are allowed for smooth handling of all the operations. However, it does not confirm the accurate encoding therefore we cannot rely solely on HTML features, and thus to encode the URLs explicitly we use the URL Encoder class of Java.

It is important to follow the given rules while encoding a string:

  1. The alphabetic characters from 'a' to 'z', or from 'A' to 'Z' and the digits from '0' to '9' remain the same.
  2. The alphanumeric characters and some special characters such as '*', '_', '-' and '.' do not change.
  3. All the spaces are converted into '+' signs.
  4. W3C recommends using "UTF-8" for encoding purposes. All the 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.

Example 1:

If the user request is such that the parameter value contains special characters and spaces as

[email protected] T point

If the encoding used is UTF-8 which is most commonly used, the '@' sign will be converted into '%40' and spaces would be converted to '+' signs. The encoded string looks like the following:

u%40Java+T+Point

Example 2:

If the user request is such that the parameter value contains no special characters and spaces as

[email protected]

The encoded string looks like the following:

u%40JavaTPoint

Encoding URL

In order to encode a URL Java URLEncoder class provides the encode() method.

URLEncoder.encode( )

The method translates a string into application/x-www-form-url encoded format using a specific encoding scheme. It obtains the bytes for unsafe characters using the supplied encoding scheme.

Syntax:

Note: The World Wide Web Consortium recommends that the UTF-8 encoding scheme should be used. Otherwise, it causes various incompatibilities.

Parameters:

s: The string to be translated.

enc: The name of the supported character encoding such as 'UTF-8'.

Returns: It returns the translated String.

Throws: UnsupportedEncodingException is thrown when the named encoding scheme is not supported.

UrlEncoder.java

Output:

URL without any encoding: 
https://www.javatpoint.com/ [email protected] java T point
URL after encoding: 
https://www.javatpoint.com/+u%40+java+T+point






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA