This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes URLEncode clarifications... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "URLEncode clarifications..." Watch "URLEncode clarifications..." New topic
Author

URLEncode clarifications...

Abraham Jacob
Greenhorn

Joined: Oct 16, 2001
Posts: 25
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

Joined: Jan 07, 1999
Posts: 6920
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.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Abraham Jacob
Greenhorn

Joined: Oct 16, 2001
Posts: 25
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

Joined: Jan 07, 1999
Posts: 6920
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

Joined: Oct 16, 2001
Posts: 25
Thanx... I think I have it. It was infact very helpful..

Regards
Abraham
 
 
subject: URLEncode clarifications...
 
Similar Threads
linking jsp pages in a liferay portlet
passing a value from one page to another
get the url in jsp
JSP Ajax append html table problem
navigation issue