I am sending the below url with query string. In the query string one parameter "approverCmt" has value with hash(#).
"/abc/efd/xyz.jas?approverCmt=Transaction Log #459505&batchNm=XS_10APR2015_082224&mfrNm=Timberland"
In server side when I tried to retrieve it from the request I get
approverCmt = Transaction Log -----> "#459505" is missing
batchNm = null
mfrNm = null
And If I remove hash(#) from query string or If I replace # with %23 every thing works fine
I don't understand why I am getting null for one parameter if another parameter contains a hash(#) symbol.
Appreciate if any one can explain.
This is known as the "fragment identifier".
As mentioned in wikipedia:
The part after the
#
is info for theclient
. It is not sent to the server. Put everything only the browser needs here.You can use the
encodeURIComponent()
function in JavaScript to encode special characters in a URL, so that#
characters are converted to other characters that way you can be sure your whole URL will be sent to the server.the hash is an anchor:
see wikipedia for more information
The Hash value is for the anchor, so it is only client-side, it is often used in client-side framework like angular for client-side routing.
The anchor is NOT available server-side.
In your case you don't need an anchor, but a parameter value with a # break the query string the value is "Transaction Log #459505".
EDIT Naive solution that doesn't work, just let it ther for history, See Real solution below
The solution is to encode client-side and decode serveur-side
Encoding in javascript
Decode in Java
EDIT: But: Javascript doesn't encode in the same way than Java So the correct answer (I hope) is to manually replace all your # with %23, then Java will decode it normally, or to use encodeURIComponent as suggested in comments. For your need the replace solution seem to be enough.
Encode in Javascript:
The decode in Java doesn't change
Sorry for long post, I didn't see the difference bettween Java and the JavaScrip Url encoding