How to do URL decoding in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
URL decoding is a crucial process in web development used to convert encoded URL components and parameters into a readable or original form. This process is necessary because URLs often need to encode certain characters — like spaces, accents, and other special symbols — into a format that can be safely transmitted over the internet. Java provides robust support for URL decoding through its standard libraries, mainly using the java.net.URLDecoder class.
Understanding URL Encoding
When data is URL-encoded, special characters are replaced with % followed by two hexadecimal digits representing the ASCII value of the character. For example, a space character (which is ASCII code 32) is encoded as %20. This encoding is essential for ensuring that the data within a URL does not interfere with the URL's structure itself.
The Role of java.net.URLDecoder
The java.net.URLDecoder class in Java is specifically designed to convert a string which has been encoded in the application/x-www-form-urlencoded MIME format back to a regular string with all special characters appropriately restored. The primary method used for this purpose is URLDecoder.decode(String s, String enc) where s is the string to be decoded and enc refers to the character encoding to use.
Steps to Decode a URL in Java
Here’s a step-by-step guide on how to perform URL decoding using Java:
- Import the Required Class Before you can use
URLDecoder, you need to import it as shown below:
- Use the Decode Method To decode a string, call the
decodemethod ofURLDecoder:
Here, "UTF-8" is the charset encoding used. UTF-8 is generally recommended as it supports all Unicode characters.
- Handle Exceptions Always handle or declare any exceptions that might occur, particularly
UnsupportedEncodingException:
Practical Example
Suppose you have a URL query string that looks like this: name=John%20Doe&age=30. If each component of the query is URL-encoded, decoding it might look like:
Summary Table
| Method | Description | Example Use | Throws |
URLDecoder.decode(String, String) | Converts %-encoded strings to normal characters. | URLDecoder.decode("name%3DJohn%20Doe%26age%3D30", "UTF-8") | UnsupportedEncodingException |
Advanced Considerations
- Character Encoding Issues: Different systems or platforms may use different character encodings. Always ensure that the decoding charset matches the encoding charset.
- Use in Modern Frameworks: In many modern Java frameworks and libraries, such as Spring or Apache HttpComponents, higher-level abstractions might already handle URL encoding and decoding, reducing the need to use
URLDecoderdirectly. - Security Concerns: When dealing with URL decoding, be wary of encoded data that might potentially lead to security exploits, such as SQL injection or XSS (Cross-site Scripting). Always validate and sanitize all input.
In conclusion, URL decoding is a pivotal process in the handling of web data, ensuring that transmitted information remains intact and usable upon receipt. Java's java.net.URLDecoder provides a straightforward and effective tool for implementing URL decoding in any Java-based application.
Related reading
- How to dockerize a Maven project? How many ways to accomplish it?
- How to dynamically turn on/off a scheduled method in a springboot application
- How to edit configmap configuration in spring boot kubernetes application during runtime
- How to efficiently remove all null elements from a ArrayList or String Array?
- How to enable all endpoints in actuator Spring Boot 2.0.0 RC1
- How to enable batch inserts with Hibernate and Spring Boot
- How to enable Bearer authentication on Spring Boot application?
- How to enable CORS in Spring Boot - Not working

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.