?
Solved

URL encoding using java

Posted on 2006-11-03
11
Medium Priority
?
539 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

770 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