Buy traffic for your website
  • Save

Java URLEncoder

Java URLEncoder
  • Save

Definition of Java URLEncoder

Java URLEncoder is a utility class that supports for the HTML form encoding. Form of HTML becomes more reliable and stable using the URLEncoder class utility of Java. Whenever a get method is called by the user the encoder appends the special characters, values, and parameters at the end of the URL which makes the URL unauthenticated in a way. Also, the value makes use of special characters which further uses the only HTML to perform smooth handling of all the operations. It is not at all a good practice to rely completely on HTML when Java URLEncoder is existing with its defined utility class.


public static String encode(String st, String enc1)throws UnsupportedEncodingException

The Syntax flow goes in a way where the parameters are as follows:

String st: This argument provides the string as an input that needs to be passed to the function String encode. String enc1: This argument provides the string enc which is used as a methodology or say procedure for encoding to be used with the string c as an argument for its working. Throws: throws keyword behaves in a similar fashion as it is a function that will be used for catching all the exceptions that may hindering the utility class of URLEncoder of Java. It mandatorily needs if the specified encoding is not used. How Java URLEncoder Class Works?

URLEncoder is a utility of a Java class that is used for any HTML encoding. Using HTML encoding every time even for small and simplified method calling is an unwanted activity when the presence of a Java utility class of URLEncoder works smoothly to enhance the type of activity for string conversion.

It Is considered as one of the safest and reliable utility class when it comes on the string and its conversion from the character parsing and encoding, decoding factors of the special string. Also, it makes use of the inbuild feature which is extensively used to convert the string into the necessary format and then some code of conduct or rules applied to that string at the time of encoding the string using URLEncoder which is as follows:

All the alphanumeric characters and certain special characters being used in the utility class such as ‘*’ ,’ _’,’ –‘ and ‘.’ remains unchanged and un notified. Allextra spaces need to get converted into ‘+’ signs. All the remaining characters within the string or other characters get encoded by individual parsing the encoded string one by one or it can be more than one byte for encoding any string as per scheme specified. Then further these bytes of string get converted into a three-character string with some string format of the form possessing the %xy, where xy represents the encoding string of characters with hexadecimal format. UTF format is mostly a recommended standard sanctioned by W3C for any kind of encoding strategies related to the string.

An example will clarify the need of UTF format standard to be followed for encoding of the string which means if we have some parameters or values which contains some values as special characters and spaces is demonstrated with an example:

u@educba for learning: If the encoded value or the character is @ then it is used by UTF-8 which is the most conventional way for accepting the encoded string type , @ symbol will get converted into 40% and the remaining values with spaces will get converted into + signs which will produce a string as an output and it will appear somewhat like: u%40educba+for+learning: Methods provided by the class as a utility for this includes only one method to serve the purpose and that method is defined as encode(). encode() is the only method supportingthis Java Utility class. As its name suggests it is the method used for encoding purpose and then returns an encoded string for any specified string. Also, it does not provide the flexibility to serve the encoding procedure smoothly with its operations. Therefore, it has created another method released in next version which has the capability even to catch hold of all the exceptions and then that encoding feature is the desired output for the utility class. Examples of Java URLEncoder

Following are the examples of java urlencoder are given below:

Example #1

This program is used to illustrate the URLEncoder utility of Java where the input string is given as the base url for the link and then a string query for retrieving the final string using UTF-8 as a conventional standard for encoding. Output is shown where one encoded string is without URL and other one with UTF-8 standard which comprises of the URL.


import; import; import; import; public class UrlEncoderJava { public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException { String baseurl = “”; String query = “u@educba for educba”; System.out.println(“Without encoding URL :”); URL url = new URL(baseurl + query); System.out.println(url); System.out.println(“URL after encoding :”); url = new URL(baseurl + URLEncoder.encode(query, “UTF-8”)); System.out.println(url); } }


Java URLEncoder-1.1
  • Save

Example #2

This program is used to represent the encoded string which makes use of the standard Charsets of the UTF_8 to the string and then provides the entire encoded string as shown in the output after converting the URL link with the defined standard and Encoder class of java.


import; import java.nio.charset.StandardCharsets; import; public class URLEncodingParsing { private static String encodingOfValue(String value) { try { return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex.getCause()); } } public static void main(String[] args) { String baseUrl = “”; String query = “educba@Java@lang”; String encodedQuery = encodingOfValue(query); String completeUrl = baseUrl + encodedQuery; System.out.println(completeUrl); } }


Java URLEncoder-1.2
  • Save


URLEncoder in java is a utility class that gets and provides aid for the HTML related forms for the encoding of the special characters being provided for parsing. The UTF-8 standard recommended by W#C has enhanced the overall encoding method for encoding and conversion of the encoded string to the final string after parsing. Using this class for string encoding is a very reliable and secured form of coding as always making HTML is not a preferred way.

Recommended Articles

This is a guide to Java URLEncoder. Here we also discuss the definition and how java URLEncoder class works? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

JSP in Java What is public in Java? Object Class in Java JSON in Java

The post Java URLEncoder appeared first on EDUCBA.

Read more:

Leave a Comment

Share via
Copy link
Powered by Social Snap