How to use Java’s URLEncoder.encode() method

8 09 2006

Content moved to




12 responses

15 09 2006

what is methods and how is implimented in java

15 09 2006

phumi, unfortunately I don’t quite understand your question. If you are asking what the URLEncoder.encode method does, please refer to the JavaDocs for the class. It basically encodes a String in order to send it through an URLConnection.
The implementation for this method is even available from Sun Microsystems and other sources. Implementations vary from JVM to JVM. Here are some references:
Sun’s implementation (download source code and go to java/net/
GNU’s implementation (gcj – source via subversion):

Hope that helps!

14 03 2007

I am so glad I found this place. I have a question about the URLEncoder.encode() method. My servlets are using it without problem, but all of a sudden my JSPs aren’t able to compile with encode(String,String). The Exception is saying that encode(String) doesn’t understand the arguments (String,String). Which is really odd, because encode(String) is deprecated. What would cause the JSPs to try to use the deprecated method?

15 03 2007

This is a strange situation because encode(String) is depracated and the sdk should give you a compile time warning, but not an exception. Are you running the JSPs and servlets on the same JVM? It looks like you’re not, although I could be wrong. What version(s) of the JVM are you using?
I would try changing your calls in the JSPs to encode(String, String) and give it a try.
Please, post back with results!

…and thanks for stopping by


27 11 2007
Amit Agrawal

Is it possible to do selective encoding for a given string. for e.g. if I don’t want colon(:), slash(/) to be encoded. How can I do so?

27 11 2007

It seems to me like what you are looking for is not URL Encoding which is a standard defined by the W3C and required for internationalization of, well, the web.

I would suggest you look at regular expressions/substitutions, or go for the source code on this link: and modify it to your needs.

Hope that helps,

25 03 2008

Hey! Thanx for the post, I found ir useful, I was looking for a serverside alternaitive to the javascript escape() or encodeURIComponent()…
I though a jsp request.encodeURL() would do the trick but no… thanx!

27 03 2008

Yep, this will do just that. Glad it helped…

30 05 2008

I wanna know how to use this URLEncoding in linux for window-1252 encoded data.
The problem is, my code takes windows-1252 character set data(for ex: BlemøVivian) which i have to URL encode(windows-1252) and post to external URL.
Here when I run the code in linux the URLEncoder.encode(“BlemøVivian”, “windows-1252”) returns “Blem%3FVivian” instead of “Blem%F8Vivian” (HEX value of ø is F8), ie in linux “BlemøVivian” is taken as “Blem?Vivian” and encodes as “Blem%3FVivian”.
Also, encoding as utf-8 returns “Blem%C3%B8Vivian”, which is not a valid url data to the server I am posting.
Any solution to handles this windows-1252 is appreciated!

30 05 2008

My thought would be that you do not want to send data that is _not_ UTF-8 encoded – you want to stick to the specification.
Never having had to deal with something like this, my thought would be

URLEncoder.encode(new String(“BlemøVivian”.getBytes(“windows-1252”)), “UTF-8”)

Not tested, not even completely sure this is your answer…give it a try though.

Maybe the java.nio.charset.CharsetEncoder class is a good place to look too.

28 08 2008

please how to use URLEncoder.encode for arabic character with appfuse 2 (struts 2).

2 04 2009
Sandeep P


I am using URLEncoder.encode() with UTF-8.The problem with the Logic i am using is..
I am using requestDispatcher to go to next JSP So In your Example You used new URL().
In case of requestDispatcher to goes to next JSP with Post data..How do i encode my date before going to Page..Can i use it with request,get/setAttribute()..?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: