Search code examples
javaurlweb-applicationsurl-encoding

What is the behavior of Hash(#) in query string


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.


Solution

  • This is known as the "fragment identifier".

    As mentioned in wikipedia:

    The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document.

    The part after the # is info for the client. 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.