Java.net.URLEncoder class in Java Last Updated : 16 Jun, 2017 Comments Improve Suggest changes Like Article Like Report 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: Alphanumeric characters and certain special characters such as '*', '_', '-' and '.' remains unchanged. Spaces are converted into '+' signs. 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- u%40geeks+for+geeks Methods : 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 // Java program to show encode() method of // URLEncoder class import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; public class UrlEncoder { public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException { // base URL String baseurl = "https://www.geeksforgeeks.org/?q="; // String to be encoded String query = "u@geeks for geeks"; System.out.println("URL without encoding :"); URL url = new URL(baseurl + query); System.out.println(url); // encode() method System.out.println("URL after encoding :"); url = new URL(baseurl + URLEncoder.encode(query, "UTF-8")); System.out.println(url); } } Output : URL without encoding : https://www.geeksforgeeks.org/?q=u@geeks for geeks URL after encoding : https://www.geeksforgeeks.org/?q=u%40geeks+for+geeks References : Official Java Documentation Comment More infoAdvertise with us Next Article Java.net.URLEncoder class in Java R Rishabh Mahrsee Improve Article Tags : Java Practice Tags : Java Similar Reads Java.net.URLDecoder class in Java This is a utility class for HTML form decoding. It just performs the reverse of what URLEncoder class do, i.e. given an encoded string, it decodes it using the scheme specified. Generally when accessing the contents of request using getParameter() method in servlet programming, the values are automa 2 min read java.net.URL Class in Java URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part 4 min read java.net.URLConnection Class in Java URLConnection Class in Java is an abstract class that represents a connection of a resource as specified by the corresponding URL. It is imported by the java.net package. The URLConnection class is utilized for serving two different yet related purposes, Firstly it provides control on interaction wi 5 min read java.net.Proxy Class in Java A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are 3 min read java.net.CacheRequest Class in Java CacheRequest class is used in java whenever there is a need for, storage of resources in ResponseCache. More precisely instances of this class provide an advantage for the OutputStream object to store resource data into the cache, in fact, This OutputStream object is invoked by protocol handlers. Ca 3 min read Like