Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLEncode clarifications...

 
Abraham Jacob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to pass some parameters as a link to another page.
Lets say I have two pages page1.jsp which contains a link to another page page2.jsp
The link that I want to pass is this
<a href="page2.jsp?niin1=123%&niin2=&itemName=test1223%" target="_self">
As you would have noticed, the link contains special characters, specifically '%'. Hence I encode it using the URLEncode.encode method from java.net lib.
So, does one encode the complete link
as in
(1)
URLEncode.encode("page2.jsp?niin1=123%&niin2=&itemName=test1223%")
OR just
(2)
"page2.jsp?" + URLEncode(niin1=123%&niin2=&itemName=test1223%)
OR
(3)
page2.jsp + URLEncode(?niin1=123%&niin2=&itemName=test1223%)

Anyway I tried all the 3 that I mentioned above.
when I tried to do it by (1) and (3) the webserver said it could not find the page.
when I tried the (2)
request.getParameter gave me null as an answer.
I am using Oracle containers for J2EE. I also tried it on my JDeveloper environment.
I know I am missing something. But what ? All help greatly appreciated ..
Thanx in advance...
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are almost right with your example 2, but in fact you need to call encode() for the value of each parameter e.g.

As a side note; it's a pretty unusual case, but if you really need strange characters in the names of your parameters, you will need to encode them too.
 
Abraham Jacob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx frank for the quick reply...
Just for the acadamic purposes, if I encode everything after the "?" that is the parameters names and the parameter values, and send them in the link, when page2 receives it, wouldn't the parameter names also be decode too, just like the parameter values are being decoded.
So I could say request.getParameter("niin1") should return me a valid value instead of a null ??
Regards,
Abraham
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case the problem is because you are also encoding the "=" and "&".
Without the "=", the servlet container doesn't know you have got any name=value parameters at all, and without the "&" it doesn't know where one parameter stops and another starts.
 
Abraham Jacob
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx... I think I have it. It was infact very helpful..

Regards
Abraham
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic