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

I am sending the following url with a query string. In the query line, one parameter, "approverCmt", has a value with hash (#).

"/abc/efd/xyz.jas?approverCmt=Transaction Log #459505&batchNm=XS_10APR2015_082224&mfrNm=Timberland" 

On the server side, when I tried to get it from the request, I get

  approverCmt = Transaction Log -----> "#459505" is missing batchNm = null mfrNm = null 

And if I remove the hash (#) from the query string or if I replace # with% 23, everything works fine

I do not understand why I get null for one parameter if the other parameter contains a hash (#) character.

Appreciate if anyone can explain.

+7
java url web-applications url-encoding
source share
3 answers

This is called a "fragment identifier".

As stated on wikipedia :

fragment identifier entered by the hash tag # is the optional last part of the URL for the document. It is commonly used to identify parts of this document.

The part after # is the information for the client . It does not go to the server. Put everything the browser needs.

You can use the encodeURIComponent() function in JavaScript to encode special characters in the URL, so that # characters are converted to other characters in such a way that you can be sure that your entire URL will be sent to the server.

+10
source share

Hash value for binding, therefore it is only on the client side, it is often used in the client structure, for example, angular for client-side routing.

Anchor is NOT available on the server side.

In your case, you do not need an anchor, but the value of the C # parameter will break the query string, this is the value "Transaction Log No. 459505".

EDIT A naive solution that does not work, just let it know the story, see the Real solution below

The solution is to encode the client side and decode the service side

Javascript coding

  encodeURI("Transaction Log #459505") //result value "Transaction%20Log%20#459505" 

Java decoding

  java.net.URLDecoder.decode("Transaction%20Log%20#459505"); //result "Transaction Log #459505" 

EDIT: But: Javascript is not encoded in the same way as Java, so the correct answer (hopefully) is to manually replace all of your # with% 23, then Java will decode it normally or use encodeURIComponent as suggested in the comments. For your need, a replacement solution seems sufficient.

Encode in Javascript:

 encodeURI("yourUrl/Transaction Log #459505").replace(/#/,"%23") //result: yourUrl/Transaction%20Log%20%23459505 

Decoding in Java does not change

 java.net.URLDecoder.decode("Transaction%20Log%20#459505") // result (java.lang.String) Transaction Log #459505 

Sorry for the long post, I did not see the difference between Java and JavaScrip Url encoding

+2
source share

the hash is an anchor:

see wikipedia for more information

+1
source share

All Articles