aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes What happens enctype= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "What happens enctype="" for URLEncoder.encode(String url, string enctype)" Watch "What happens enctype="" for URLEncoder.encode(String url, string enctype)" New topic
Author

What happens enctype="" for URLEncoder.encode(String url, string enctype)

rajesh babu Y
Ranch Hand

Joined: Nov 12, 2010
Posts: 31
Hi All,
Could you please let me know the difference between URLEncode.encode(Staring urlname) and URLEncoder.encode(String url, ""<Not specified any encrypt type>) from 1.4 to 1.5...

Thanks,
Rajesh Yarlagadda
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

When you don't specify the encoding the system default encoding will be used. While that may not be a problem for files (usually), on the web you cannot make such assumptions. That's why the overloaded method without an explicit encoding has been made deprecated.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
rajesh babu Y
Ranch Hand

Joined: Nov 12, 2010
Posts: 31
For URLEncoder.encode(string url) takes the UTF-8 as default encoding type, then now, if i go with URLEncoder.encode(string url, ""<not specifying any encoder type, just gave empty string>) is it will take the default or not perform the operation ?

from the java api docs i read that URLEncoder.encode(string url) takes if an encoding is not specified, then the default encoding of the platform is used. How the encoding is differentiated from each platform ?

Thanks,
Rajesh
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

rajesh babuy wrote:For URLEncoder.encode(string url) takes the UTF-8 as default encoding type

No it doesn't. It takes the system's encoding. On Windows that's usually Windows-1252 which is closely related to ISO 8859-1. It's definitely not UTF-8.

then now, if i go with URLEncoder.encode(string url, ""<not specifying any encoder type, just gave empty string>) is it will take the default or not perform the operation ?

If you provide an empty String then I expect (and actually get) an UnsupportedEncodingException. You need to provide a supported encoding, e.g. UTF-8.

from the java api docs i read that URLEncoder.encode(string url) takes if an encoding is not specified, then the default encoding of the platform is used. How the encoding is differentiated from each platform ?

For me it would be Windows-1252. For someone in Russia using Windows it's more likely to be Windows-1251. It all depends on the operating system and system language. Because the encoded values are usually sent between systems with possible different encodings, it's very unwise to use the system default encoding. By explicitly specifying the encoding you will ensure that both sides will use the same encoding.
rajesh babu Y
Ranch Hand

Joined: Nov 12, 2010
Posts: 31
I'm working on one application which is now running on unix machine, webshpere 5.0 server with java 1.4 and j2ee1.3. In my code we are using URLEncoder.encode(String url).

Now i have requirement to change the application configuration to another(like linux, Webshpere 6.1, java 1.5 and j2ee 1.4 ). when i compile the java files i got the warning URLEncoder.encode(String url) is deprecated.

we are working on windows machines and the application located in linux machines. how do i come to know which encoding type it is using ?

If i gave URLEncoder.encode(String url, String encodetype<UTF-8>) and deployed my application on linux machines what happens, is it throws UnsupportedEncodingException or is it take the platform specific encoding type? is there any way to take the default encoding type base on platform ?

Sun systems won't provide any default encoding type, it's based on platform. How i can move ahead to get my application without warn messages from these deprecated methods?

Thanks,
Rajesh

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

The method has been deprecated since Java 1.4, so you should have seen the deprecation warning before. As for the default encoding, you can see that by calling Charset.defaultCharset().name().
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What happens enctype="" for URLEncoder.encode(String url, string enctype)