• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

URL Encoding Spaces with Percent (%20) Instead of Plus Sign (+)

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,

How can I do URL encoding on spaces using percent symbols instead of the plus sign the way Java does it?

I need to encode a text field that is used in a URL. But when it's displayed back by the web page, it needs to be readable.

Test String: TEST with special characters ~ ` ! @ # $ % ^ & * ( )  _ - + = [ ] { } \ | ; : ' " , . < > / ?

When I encoded this using Java's URLEncoder.encode(String test), it replaced the spaces with plus signs (+). This was the result when the string was returned on the web page:

Result: TEST+with+special+characters+~+`+!+@+#+$+%+^+&+*+(+)++_+-+++=+[+]+{+}+\+|+;+:+'+"+,+.+<+>+/+?

But it's ugly and users can't read it. When using %20, the spaces are properly displayed on output. Also, notice the section with three plus signs in a row (+++), which is the space and which is the plus sign?

Is there a way to force using %20 in Java, or is there another recommended library for doing this encoding?
 
Sheriff
Posts: 27456
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest looking at whatever it is that's displaying that text on the web page. Better still, I'd suggest asking why a URL-encoded string even needs to be displayed on a web page.
 
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/net/URLEncoder.html
 
Wayne Woodrow
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I didn't explain correctly. The text field is passed to another system that I don't have control over in the URL. That application takes the URL parameters and displays them on a web page. This is for our ticketing and survey systems which are two separate applications. The survey system displays the survey form, and uses the URL parameter to display things like created date, customer name, and in the case the title.

This worked perfectly using Javascript's encodeURIComponent() function which encodes with %20. However, it's a little problematic doing it on the client side, so I wanted to handle it with Java.

Kristina thank you for the doc link. I've been looking through them though, and very well could have missed it, but I couldn't find anything on how to use percents instead pluses for the spaces.
Here is the line I'm using in my program:
 
Paul Clapham
Sheriff
Posts: 27456
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wayne Woodrow wrote:The text field is passed to another system that I don't have control over in the URL. That application takes the URL parameters and displays them on a web page.



So, just to clarify, you are sending the text field to that system by sending an HTTP GET request to it, and the text field is one of the parameters of the GET?

If so then the problem is that the other system isn't doing URL-decoding properly. You might consider complaining to them.

Or you could take the passive-aggressive route and ask them, since the encoding system they are requiring isn't standard URL-encoding, to send you the specs for the actual encoding they are using.

However what I would do at this point, knowing that either of those two approaches wouldn't be that helpful, would be this:

1. URL-encode the string.

2. Convert all the + signs in the result to %20.
 
Wayne Woodrow
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup the text field is sent in the get request to the survey system.

Hmm, good point about the survey system not properly decoding. I think it should be turning those + signs back into spaces. I'll look into their resources to see if there's another solution also.

As far as converting the + to %20, I think that will do it! I ran a test, and it looks like it's displaying correctly.

 
Saloon Keeper
Posts: 26291
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "+" form is actually preferred, since, while technically no one should be reading URLs, sometimes they do, and in particular, if you want to type in a URL, it's a lot more convenient to type one "+" for a space than it is to type "%20". And of course, simply typing an actual space character would otherwise syntactically indicate the end of the URL. The "+" shorthand was more important back in the days when web browsers were still new and often not even graphic.

More important is that since "+" is a reserved character, you do need to encode it in URLs (%2B). Otherwise, it would be assumed to be an encoded space.
reply
    Bookmark Topic Watch Topic
  • New Topic