Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Some problem in JSP

6 views
Skip to first unread message

Suresh Athmakuri

unread,
Mar 28, 2002, 2:15:11 PM3/28/02
to
Hi
I have a small basic question on jsp What is the diffrence between

String variable=null;

1.out.println("Some String"+variable);
2.<%=variable%>

since in the first case i see "nul"l displayed and other case i see a
"blank" value.

regards
suresh

Dingo Girl

unread,
Mar 28, 2002, 3:51:50 PM3/28/02
to

The reason is simple. When you do:
out.println("Some String"+variable);
The variable *value* is printed out

<%=variable%> evaluates the variable value as a String. It would be like out.println("Some
String" + variable.toString());

Hope this helps!

Leilani


Robert Patrick

unread,
Apr 5, 2002, 6:47:48 PM4/5/02
to
No, I would call this a bug. The problem is that the out.println() code
generates the following Java code in the servlet:

jspwriter.println("Some String = " + variable);

and the JSP tag generates:

jspwriter.print(StringUtils.valueOf(variable));

The StringUtils.valueOf(Object obj) method looks like this:

return obj != null ? obj.toString() : "";

I would argue that it should look like this so that they are the same:

return obj != null ? obj.toString() : "null";

I would file a case with support...
Robert

d...@dima.dhs.org

unread,
Apr 5, 2002, 7:50:10 PM4/5/02
to
This was probably added as a convinience thing, so

<%= variable %> will result in a blank instead of 'null',

but it creates problems when porting to other containers which
do not do this, and WebLogic JspWriter implementation is not doing
what it supposed to do according to Sun's javadoc:

---
javax.servlet.jsp.JspWriter.print(String s)
Print a string. If the argument is null then the string "null" is printed.

javax.servlet.jsp.JspWriter.print(Object obj)
Print an object. The string produced by the String.valueOf(Object) method is
translated into bytes according to the platform's default character encoding,
and these bytes are written in exactly the manner of the Writer.write(int) method.

and java.lang.String.valueOf(Object obj)
Returns:
if the argument is null, then a string equal to "null"; otherwise, the value
of obj.toString() is returned.
---

> jspwriter.print(StringUtils.valueOf(variable));

> Dingo Girl wrote:

--
Dimitri


Robert Patrick

unread,
Apr 5, 2002, 11:12:32 PM4/5/02
to
I have reported this as a bug. We'll see what they say...

Robert Patrick

unread,
Apr 18, 2002, 12:14:59 PM4/18/02
to

Okay, so here's the deal.

Since WLS has always printed "" instead of "null", we are not planning to fix this
in 6.1 or earlier releases. The main motivation for printing "" was to make it easier
for the JSP programmer so that they could do this:

<%= foo %>

instead of:

<%= (foo != null) ? foo : "" %>

In WLS 7.0, we have changed the default behavior to print "null" instead so as to
be strictly JSP 1.2 compliant but have added a "printNulls" element to the deployment
descriptor to allow you to revert to the old behavior.

Hope this helps,
Robert

0 new messages