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
Solved

URL encoding using java

Posted on 2006-11-03
11
534 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 50
jsp login check 12 42
Java syntax, or is it Selenium 6 30
swing controls 2 16
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

809 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