Java String Request Parameter ignoring #

380 Views Asked by At

Am new to Java.

Am appending a new parameter at the end of the URL as below.

http://localhost:8080/BugReport/home?projectName=Thrivent:PS_Thrivent_SOW#04_YSL1.1AggFL&Consulting

Am reading this in my servlet as below with request.getParameter

String projectNameStr = request.getParameter("projectName");

Am observing a strange behaviour. If i pass # symbol along with the parameter then projectnameStr is printing only to that symbol. i.e, Thrivent:PS_Thrivent_SOW

Am not able to read the complete string if the # symbol is present in the parameter?

How to solve this problem? Please someone help me here.

2

There are 2 best solutions below

0
David On

Because everything after a # is a URL Fragment and is not sent to the server, so the server can't read it.

URL-encode your values to prevent making use of otherwise reserved-characters:

http://localhost:8080/BugReport/home?projectName=Thrivent%3APS_Thrivent_SOW%2304_YSL1.1AggFL%26Consulting

On the server-side some frameworks may URL-decode the value for you, or you may have to do it manually. Testing in your code should verify that.

2
Kevin Hooke On

'#' is a special character and needs to be url encoded. See https://www.urlencoder.io/learn/

If you replace it with %23 as it's encoded equivalent then your code will work as expected.

Note: if you're expecting to pass other special characters as literal String values then you'll need to encode those chars too, e.g. '&'