-1

I have the URL with the following query string

equipmentAccessoryRoute=LFVR+BASICACC

When I do request.getParameter("equipmentAccessoryRoute") it returns 'LFVR BASICACC' in a string variable, replacing plus sign with space.

To resolve this issue I did something like this

String accessoryRoute = java.net.URLEncoder.encode(request.getParameter("equipmentAccessoryRoute"),"UTF-8");

It worked perfectly but now yt is not working for the following query string (which was working before)

`equipmentAccessoryRoute=C1000IP5EL@-A`

Decoding converts this into 'C1000IP5EL%40-A' and stores into a string.

I am really confused. I tried to learn URL encoding but find it very hard to understand.

user207421
  • 305,947
  • 44
  • 307
  • 483
Adeel
  • 413
  • 1
  • 7
  • 22
  • Are you trying to **encode a URL?** – Momoro Feb 04 '20 at 03:33
  • That is what `URLEncoder.encode()` is supposed to do, see [URL encoding](https://en.wikipedia.org/wiki/Query_string#URL_encoding). – Andreas Feb 04 '20 at 03:34
  • Yes, I agree @Andreas - I don't see your problem, **Adeel**. – Momoro Feb 04 '20 at 03:35
  • @Momoro: I just want to keep '+' and '@' sign from query parameter into my java string. – Adeel Feb 04 '20 at 03:35
  • `LFVR+BASICACC` is the encoded version of `LFVR BASICACC`. `LFVR%2BBASICACC` is the encoded version of `LFVR+BASICACC`. The values in your query string are supposed to be **encoded**. – Robby Cornelissen Feb 04 '20 at 03:35
  • 2
    @Adeel But they are not valid characters in a query string. See the link I provided. – Andreas Feb 04 '20 at 03:35
  • 1
    You can check out https://stackoverflow.com/questions/332872/encode-url-in-javascript and https://www.w3schools.com/jsref/jsref_encodeuri.asp – Momoro Feb 04 '20 at 03:35
  • 1
    @Adeel You have to encode your URL at client side for keeping the `+` and `@` sign in java string. – Ganesh Patel Feb 04 '20 at 03:36
  • This is not an 'issue'. It is correct behaviour. If you want the `+` you have to encode it in the URL argument. And what `request.getParameter()` is doing is *decoding*, not encoding. – user207421 Feb 04 '20 at 04:38

1 Answers1

-1

URL - Uniform Resource Locator

  • URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
  • URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
  • URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

Hope this helps.

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24