0

I have 3 parameters in my URL. And I have encoded them using URLEncoder.encode(myUrl,"UTF-8") My url with encoded parameters looks like this

http://localhost/myPage.jsp%3Fparam1%3Daction%26param2%3D3%26param3%3Dhi  

I specified pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" in my jsp page and also set request.setCharacterEncoding("UTF-8") before using request.getParameter("param1") to get parameter value.

Still I only get the value of first param i.e param1. For other params I get null

But if I do double encoding(using URLEncoder.encode(myEncodedUrl,"UTF-8")) I can get all three parameters' values. I guess double encoding is not the correct way to do it.

If I replace the & with %2526 instead of %26(actual encoded value of &) I am getting value of all 3 params. I guess its not correct either.

Please let me know what I am missing in first place.

Arun
  • 825
  • 4
  • 14
  • 31
  • I decoded your URL and it looks like this `http://localhost/myPage.jsp?param1=action¶m2=3¶m3=hi`. Can you confirm is that is the characters you intended? – Minh Kieu Jun 13 '17 at 07:58
  • Yes. I too get that if I decode it. But When I try to use `request.getParameter("param2")`, it gives me `null`. – Arun Jun 13 '17 at 08:44
  • 1) Your url encoding is incorrect. `&` needed to separate each parameters. However if your using custom separator then you need to parse the URL yourself. 2) To get the second parameter if `&` used, you just need to do `request.getParameter("m2")` – Minh Kieu Jun 13 '17 at 10:16
  • I already have `&` to separate params. In my URL, if I replace `&` with `%2526` I get values of all 3 params. – Arun Jun 13 '17 at 11:27
  • Jayjay has provided the answer. – Minh Kieu Jun 13 '17 at 12:40

1 Answers1

1

You're encoding the URL wrong, you shouldn't encode the whole URL:

?param1=action&param2=3&param3=hi

You should only encode the values of the parameters: 'action', '3'(if it's not an integer) and 'hi'.

Maybe this link will help you. Java URL encoding of query string parameters

Jelte
  • 197
  • 3
  • 19