Hi Oren,
--- "Oren Deri, Nice-Eye" <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> when printing in jsp some String that is null
> (String str = null;)
> the output html is the string "null".
> Look at the code of JspWriterImpl.print(String s)
>
> public void print(String s) throws IOException {
> if (s == null) {
> s = "null";
> }
> write(s);
> }
>
> why did you return "null"? I changed it to "" insted
> of "null" and it works
> just fine.
> :
It may 'work' just fine, in that it is not crashing,
or anything drastic, but the change you made is not
correct. The proper String representation of a null
Object reference is "null". This is part of the Java
Language Specification [sec 15.18.1.1]:
"If the reference is null, it is converted to the
string "null" (four ASCII characters n, u, l, l).
Otherwise, the conversion is performed as if by an
invocation of the toString method of the referenced
object with no arguments; but if the result of
invoking the toString method is null, then the string
"null" is
used instead."
The rational for this is very simple: A null
reference is not an empty String. An empty String is
not a null reference. Consider the following case:
String s = null;
Boolean b = null;
Map m = new HashMap();
m.put("key1",s);
m.put("key2,b);
System.out.println("key1 = "+m.get("key1"));
System.out.println("key2 = "+m.get("key2"));
Now, in both cases, I have stored IDENTICAL values
(null). (s==b)==true. As far as the map m is
concerned, s and b are both of type Object and have
identical reference values of null. Thus, they should
both print out identical String representations (and
they do: the characters n-u-l-l).
>
> public void print(String s) throws IOException {
> if (s == null) {
> s = "";
> }
> write(s);
> }
>
> My questions:
>
> Is my change can hurt the tomcat operation?
Yes because it is no longer in spec.
> Why did you return "null" and not ""
> (the original software is on weblogic but we moved
> it to tomcat which have
> better performance, in weblogic they return "" and
> not "null") ?
>
Unfortunately, the WebLogic JSP implementation is not
within specification. It is broken. While this
change provides a convenience for the lazy JSP
developer, this deviation results in the writing of
JSP code that is not portable to other JSP compilers.
We were bitten by this same misbehavior this winter as
we ported a large body (several hundred pages) of code
from WLS to tomcat/jasper. We were forced to rewrite
zillions of lines of code by our developers (despite
repeated warnings about this) who failed to do the
simple task of checking for null values in their code.
Basically, if you want to use an empty String to
represent a null value, you should do something like:
s==null?"":s
in your code.
Note - we HAD to move off the WLS JSP compiler for
other more fundamental technical reasons as well.
I hope this explains things a bit for you.
Mel
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - Buy the things you want at great prices.
http://auctions.yahoo.com/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]