Problem with getParameter if parameter contains special characters.

Hello,

From a jsp file, I call, via Javascript, another page.

url='foo.jsp?rand='+myRand+'param1="+param1;

alert("url="+url);

document.forms[0].action=url;

document.forms[0].submit();

The alert shows that the url is correctly built.

Now, int the foo.jsp file, I have:

String param1= request.getParameter("param1");

System.out.println("param1="+param1);

THe problem is that "param1" contains the special character #.

So, when foo.jsp gets the parameter, it does not get it entirely but till it sees the # !!!

How can I get round this problem?

Thanks

[763 byte] By [celia05esa] at [2007-11-27 5:09:39]
# 1
url='foo.jsp?rand='+myRand+'&param1="+param1;
bsampieria at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...
# 2
and don't mix ' and " (assuming that's not a typo)
bsampieria at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...
# 3

Shouldn't you be using an ampersand to split your parameters (assuming the parameter param1is a different one)?

url='foo.jsp?rand='+myRand+'&param1="+param1;

alert("url="+url);

> THe problem is that "param1" contains the special

> character #.

Switch it out with "%23".

phone# => phone%23

http://reference.lassosoft.com/Reference.LassoApp?utility/codetable

kevjavaa at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...
# 4

I cannot switch to %23 since I don't know the contents before hand... what it it contains other special characters?

THe thing is that when the url is written (through the alert command), I can see that the parameter is correct and complete.... but it contains one #.

Now, when the other page is loaded and when I try to get the parameter (using getParameter().... the string I get back is incorrect and stops before the # character!!

What can I do to avoid it?

PS: The url error was a tipo, sorry!

celia05esa at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...
# 5

> I cannot switch to %23 since I don't know the

> contents before hand... what it it contains other

> special characters?

> THe thing is that when the url is written (through

> the alert command), I can see that the parameter is

> correct and complete.... but it contains one #.

> Now, when the other page is loaded and when I try to

> get the parameter (using getParameter().... the

> string I get back is incorrect and stops before the #

> character!!

> What can I do to avoid it?

>

> PS: The url error was a tipo, sorry!

From http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html :

The URL class does not itself encode or decode any URL components according to the escaping mechanism defined in RFC2396. It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL. Furthermore, because URL has no knowledge of URL escaping, it does not recognise equivalence between the encoded or decoded form of the same URL. For example, the two URLs:

http://foo.com/hello world/ and http://foo.com/hello%20world

would be considered not equal to each other.

Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL().

So, if you use the [url=http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html]URI[/url] class, you can encode all that junk and get a good string to pass across the ether.

kevjavaa at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...
# 6
But in this case the URL-encoding would have to be done by the Javascript code, wouldn't it? I haven't got to that part of my Javascript book yet but I would expect there's a Javascript function that does URL-encoding.
DrClapa at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...
# 7

> But in this case the URL-encoding would have to be

> done by the Javascript code, wouldn't it? I haven't

> got to that part of my Javascript book yet but I

> would expect there's a Javascript function that does

> URL-encoding.

Indeed, if you need to do this from Javascript, you use escape():

http://www.w3schools.com/jsref/jsref_escape.asp

Thanks, DrClap.

kevjavaa at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...
# 8
Yes, I checked and there is a function called escape .... it has done the job !!! THanks!
celia05esa at 2007-7-12 10:29:30 > top of Java-index,Java Essentials,Java Programming...