• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue faced during request.getParameter()

 
Naveen Kumar Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am facing one issue when passing a parameter in a http request and trying to retrieve the values in the servlet.
Inside one index.jsp, I have a field /parameter with value as below:

Sample code:
<%

URLDecoder urlDecoder = new URLDecoder();
String str1="";
str1=urlDecoder.decode("qDqKNTYGxzvhVXsFgpX%2BZe4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE%0D%0AFsy7jHC%2FYLXW");
System.out.println(str1);

str = "http://localhost:8080/WebApp2/Servlet2?iburl="+str1;
System.out.println(str); //prints : qDqKNTYGxzvhVXsFgpX+Ze4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE
Fsy7jHC/YLXW
%>

I have set this value in a parameter called "myParam"in the in form action url, as
http://localhost:8080/WebApp2/Servlet2?myParam="+str1

In servlet2, ideally when i retrieve the value as: request.getParameter("myParam"); it should print the same output as above in the jsp, howver, issue is, it is printing as below:
qDqKNTYGxzvhVXsFgpX Ze4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE

I would like to know why is request.getParameter("myParam"); not able to fetch the exact value passed in the http url. If it has a default decoding behavior, how to fetch the actual value which is passed in the url?

Appreciate and thanks in advance for your suggestions/solutions.
Cheers.

 
Kiran Dama
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen,

try using the other method

URLDecoder.decode(String s, String enc)
Decodes a application/x-www-form-urlencoded string using a specific encoding scheme.

here you pass the encoding of your choice.

for eg.
str1=URLDecoder.decode("qDqKNTYGxzvhVXsFgpX%2BZe4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE%0D%0AFsy7jHC%2FYLXW", "ISO-8859-1");

Hope this helps.

Best Regards,
Kiran
 
Naveen Kumar Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kiran for your suggestion. I tried below combinations, however same output is getting printed.
Input:

1: str1=urlDecoder.decode("qDqKNTYGxzvhVXsFgpX%2BZe4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE%0D%0AFsy7jHC%2FYLXW","UTF-8");
2: str1=urlDecoder.decode("qDqKNTYGxzvhVXsFgpX%2BZe4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE%0D%0AFsy7jHC%2FYLXW","ISO-8589-1");


System.out.println("value:"+request.getParameter("iburl"));

String param = URLDecoder.decode(new String(request.getParameter("iburl").getBytes("iso-8859-1")), "iso-8859-1");
System.out.println("param1:"+param);

param = URLDecoder.decode(new String(request.getParameter("iburl").getBytes("UTF-8")), "UTF-8");
System.out.println("param2:"+param);


In all cases it prints: qDqKNTYGxzvhVXsFgpX Ze4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE
 
Kiran Dama
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen,

Can you post the exact output as you get in the console.
I am getting the correct result as follows:

param1:qDqKNTYGxzvhVXsFgpX+Ze4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE
Fsy7jHC/YLXW

Note that %0D stands for carriage return.

 
Naveen Kumar Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kiran,

I am printing the output in a servlet, through request.getParameter() as below, normal java program print works..how in the servlet when you retrieve from the request object through getParameter, it is not.
Let me know if you are printing in the servlet...?

System.out.println("value:"+request.getParameter("iburl"));

 
Kiran Dama
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen,

Even I am printing in the servlet

System.out.println("param1:" + request.getParameter("str"));

output

param1:qDqKNTYGxzvhVXsFgpX+Ze4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE
Fsy7jHC/YLXW

You problem is related to URL encoding

can you try printing Characterencoding

System.out.println(request.getCharacterEncoding());


 
Naveen Kumar Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kiran,

You are right. I was sending the Url with decoded version.

str1=urlDecoder.decode("qDqKNTYGxzvhVXsFgpX%2BZe4u7Pu6bTdU9P9KQonpEgZXAByZ8xlrvOVuShZVXbNkBnQtsCLsqdlE%0D%0AFsy7jHC%2FYLXW");

instead of the actuall value.

Thanks for pointing me to check the values again. Cleared that and workin now.

Cheers...!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic