Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Jan 16, 2007

Using the URLEncoder/URLDecoder Classes

To include special characters such as spaces and quotes in a URL string, you need to encode them. Similarly, you need to decode encoded characters in a URL to recover the original URL. The following code demonstrates how to code/decode a string using the URLEncoder/URLDecoder classes.

import java.net.*;

public class URL_Encoder_Decoder{

public static void main(String[] args){

  String sir="@sir de caractere@,nr. 1290 'paragraf 3'";
  try{
     String sir_codat=URLEncoder.encode(sir,"UTF-8");
     String sir_decodat=URLDecoder.decode(sir_codat,"UTF-8");
     System.out.println(sir);
     System.out.println(sir_codat);  
     System.out.println(sir_decodat);      
     }catch(java.io.UnsupportedEncodingException e)
         {System.out.println("Eroare:"+e.getMessage());}            
  }
}
Here's the result:

@sir de caractere@,nr. 1290 'paragraf 3'
%40sir+de+caractere%40%2Cnr.+1290+%27paragraf+3%27
@sir de caractere@,nr. 1290 'paragraf 3'
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap