This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Issue faced during request.getParameter() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Issue faced during request.getParameter()" Watch "Issue faced during request.getParameter()" New topic
Author

Issue faced during request.getParameter()

Naveen Kumar Kumar
Greenhorn

Joined: Jul 28, 2010
Posts: 19
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.


Push the limits to push yourself..!!
Kiran Dama
Greenhorn

Joined: Jan 09, 2013
Posts: 3
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

Joined: Jul 28, 2010
Posts: 19
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

Joined: Jan 09, 2013
Posts: 3
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

Joined: Jul 28, 2010
Posts: 19
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

Joined: Jan 09, 2013
Posts: 3
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

Joined: Jul 28, 2010
Posts: 19
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...!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue faced during request.getParameter()
 
Similar Threads
How to retrieve value from a link
Please guide me
Problem in calling a method in an EJB from a WCS 5.1 command
compiling and deploying into Tomcat
Display table row value in another form