Java URL EncoderJava 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:
Example 1: If the user request is such that the parameter value contains special characters and spaces as u@Java 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 u@JavaTpoint 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/ u@ java T point URL after encoding: https://www.javatpoint.com/+u%40+java+T+point Next TopicanyMatch() in Java 8 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India