You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "Oren Deri, Nice-Eye" <or...@nice-eye.com> on 2001/03/08 11:30:06 UTC

Problem with JspWriterImpl

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.
:

public void print(String s) throws IOException {
	if (s == null) {
	    s = "";
	}
	write(s);
    }
 
My questions:

Is my change can hurt the tomcat operation?
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") ?


10x 

Oren, Israel



-----------------------------------------
Oren Deri
Cellular: 972-53-555579
Mail: oren@orenderi.com
------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-dev-help@jakarta.apache.org


RE: Problem with JspWriterImpl

Posted by Carlos Gaston Alvarez <ga...@tournet.com.ar>.
Well, if you changed the code and it works for you, it is fine.
Now, the main diference is that "" hides the error while "null" does not. I
mean "" is not equal to null.
Also, printing "null" follows better the java philosophy. Remember to make
the change each time you install a new version. But I think that it is a
better idea to modify your code so you controll if it is a null.

    out.print( (yourVariable == null) ? "" : yourVariable );

This way it will be fully portable.


Chau,

Carlos Gaston Alvarez

ps: someone please check that peace of code. I mean, java core classes
should be doing this automatlically for us. I think we are duplicating code.


----- Original Message -----
From: Oren Deri, Nice-Eye <or...@nice-eye.com>
To: <to...@jakarta.apache.org>
Sent: Thursday, March 08, 2001 7:30 AM
Subject: Problem with JspWriterImpl


>
> 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.
> :
>
> public void print(String s) throws IOException {
> if (s == null) {
>     s = "";
> }
> write(s);
>     }
>
> My questions:
>
> Is my change can hurt the tomcat operation?
> 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") ?
>
>
> 10x
>
> Oren, Israel
>
>
>
> -----------------------------------------
> Oren Deri
> Cellular: 972-53-555579
> Mail: oren@orenderi.com
> ------------------------------------------
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-dev-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-dev-help@jakarta.apache.org


Re: Problem with JspWriterImpl

Posted by Mel Martinez <me...@yahoo.com>.
Hi Oren,

--- "Oren Deri, Nice-Eye" <or...@nice-eye.com> 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: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-dev-help@jakarta.apache.org


RE: Problem with JspWriterImpl

Posted by Marc Saegesser <ma...@apropos.com>.
This is the expected operation of a PrintWriter.  Read the JavaDoc for
java.io.PrinterWriter.print(String s).

print

public void print(String s)

Print a string. If the argument is null then the string "null" is printed.
Otherwise, the string's characters are converted into bytes according to the
platform's default character encoding, and these bytes are written in
exactly the manner of the write(int) method.
Parameters:
s - The String to be printed

> -----Original Message-----
> From: Oren Deri, Nice-Eye [mailto:orendi@nice-eye.com]
> Sent: Thursday, March 08, 2001 4:30 AM
> To: tomcat-dev@jakarta.apache.org
> Subject: Problem with JspWriterImpl
>
>
>
> 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.
> :
>
> public void print(String s) throws IOException {
> 	if (s == null) {
> 	    s = "";
> 	}
> 	write(s);
>     }
>
> My questions:
>
> Is my change can hurt the tomcat operation?
> 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") ?
>
>
> 10x
>
> Oren, Israel
>
>
>
> -----------------------------------------
> Oren Deri
> Cellular: 972-53-555579
> Mail: oren@orenderi.com
> ------------------------------------------
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-dev-help@jakarta.apache.org