esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I encode and decode a URL?
When working with the HTTP protocol, some type of encoding is necessary because certain characters that are communicated over the network have special meaning for webservers, like @ and ?. For example, if you create a FORM in HTML using the GET or POST method, the webserver needs to differentiate between the different elements available on the form, etc. URL encoding is the most popular encoding scheme used for these types of things. It's MIME format is called "x-www-form-urlencoded".

The scheme for encoding is very simple (from the API):

 
This code sample is only viewable to esus.com members
Login or become a member!



Main.java:

This code sample is only viewable to esus.com members
Login or become a member!


outputs:

 
This code sample is only viewable to esus.com members
Login or become a member!


Try changing the string "hello world" with a complex url!

Notice that the method URLDecoder.decode throws an Exception in JDK1.2.2 (but it actually never throws one). This was removed in 1.3 and may affect compiling older code, as you had to catch the exception before 1.3. See the bug at the link below.


Further Information
Author of answer: Joris Van den Bogaert

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.