Solved

URL encoding using java

Posted on 2006-11-03
11
532 Views
Last Modified: 2012-05-05
Hi
When I encode a string value using URLEncoder.encode(strName) it replaces the white spaces in the string with "+" symbol.
However, want it to replace the white spaces to "%20" instead of "+" symbol so that it would be compatible across browsers and with JavaScript escape() and unescape() functions.

Is there a way to force URLEncoder to use "%20" instead of "+" signs?

Thanks in advance.

Regards,
0
Comment
Question by:ashishanand
  • 7
  • 4
11 Comments
 
LVL 14

Expert Comment

by:hoomanv
ID: 17864830
You'll have to do it yourself by replacing all + signs with %20
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17864841
str = str.replaceAll("\\+", "%20");
0
 

Author Comment

by:ashishanand
ID: 17864917
I understand that it can be done by using replaceAll() however, I'm concerned with encoding / handling of special characters.

If we use replaceAll() , we would need to handle each of the special characters explicitly and this means more work (including testing task).

I'm looking for a generic solution.

Regards,
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17864971
You are only concerned about one special char: space
You can correct it by including only one more line
0
 

Author Comment

by:ashishanand
ID: 17864996
Hi hoomany,

I'm not clear by what you have just said. Are you saying that I should be only concerned with just white space.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:hoomanv
ID: 17865010
Talking clearly

> Is there a way to force URLEncoder to use "%20" instead of "+" signs?
Not that I'm aware of

However you can first url encode your url
Then replace all undesirable + signs with %20

> we would need to handle each of the special characters explicitly
You don't need, only white space as you are concerned about
0
 

Author Comment

by:ashishanand
ID: 17865073
As mentioned earlier, I'm not only concerned with white space. I'm also concerned about other special character.

Are saying that we should only be concerned about handling white space while working with java URLEncoder and javascript unescape.
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17865129
> I'm also concerned about other special character
Could you name some of them ?
0
 

Author Comment

by:ashishanand
ID: 17865189
Some of the special characters are &*<>/?-_%'".\/|{}[]
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17865201
And URLEncoder is responsible for all of them

URLEncoder.encode("&*<>/?-_%'.\\/|{}[]", "UTF-8"); --> %26*%3C%3E%2F%3F-_%25%27.%5C%2F%7C%7B%7D%5B%5D

So, You only need to replace + signes to %20
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 125 total points
ID: 17865211
Original + signs will be converted to %2B
And spaces will become +
Then just call replaceAll("\\+", "%20");
and those + will become %20 too
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 102
Non-recursive backtracking, using a stack 1 103
Cipher Configuration on Apache HTTPD 4 53
Error trying to install RTMT Win7 5 41
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now