Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

URL encoding using java

Posted on 2006-11-03
11
Medium Priority
?
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 375 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

604 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