-1

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 Answers2

0

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.

David
  • 208,112
  • 36
  • 198
  • 279
0

'#' 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. '&'

Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33
  • Thanks Both ! I tried with the below. its not working. Can you help please? String projectNameStr = request.getParameter("projectName"); URL myUrl = new URL(projectNameStr.replace("#","%23")); – Bhargava Hg Jul 08 '20 at 18:03
  • You need to do the replacement in the url in your browser, not in your app code. The url itself is not well formed. Where is the url coming from in the first place, is it a link in a page? Who is building the url? Whatever is building the url needs to urlencode the special characters in the url. If you're manually typing it in to your browser, you need to url encode the special chars yourself – Kevin Hooke Jul 08 '20 at 19:01