Solved

URL encoding using java

Posted on 2006-11-03
11
531 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

13 Experts available now in Live!

Get 1:1 Help Now