• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLEncoder class ?

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does this method static encode() of the class URLEncoder do ?
what is this translation of a string into x-www-form-urlencoded ??
in one of the examples its usage is like this:
String ss = URLEncoder.encode(args[0]);// the user enters some string at command line
pw.println("string is : "+ss);//where pw is PrintWriter object
here is the api
encode(String s)
Translates a string into x-www-form-urlencoded format.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is meant for URL encoding the string. When URLs contain a query string, (name=value pairs), the values are encoded using this method. URL encoding includes converting spaces to + or %20 and all other non alpha-numeric characters are converted to their HEX value, which is of the form - %XX, where XX is the HEX value. So my name Manish Hatwalne becomes either Manish+Hatwalne or Manish%20Hatwalne. This can also safely pass values such as /, // and . (period) which have special meaning in the URL.
Of course, there's a corresponding decode method for this. I believe this information is documented in the API as well.
HTH,
- Manish
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but why do you need to URLEncode a string, in the first place. What good does it serve?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to URL encode Strings to put them on a URL, otherwise you could add data that acts strangely or creates illegal URLs.

eg it fixes this:


and it stops this behaving badly:


Dave
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ed Bicker:
Yes, but why do you need to URLEncode a string, in the first place. What good does it serve?


You would normally not need them because browser does this encoding when it issues a GET request. But in case you are using applets and issuing the GET request programmatically, then you would have to encode() them before it reaches the server.
reply
    Bookmark Topic Watch Topic
  • New Topic