• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on jsp:useBean

 
Satheesh
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I have a question on jsp:useBean, I have a simple bean with two instance variables, one is of type String ( eg :name) and other is of type int ( eg:age).
Note : both vairables are not initilized
<jsp:useBean id="user" class="package.ABC"/>
Name :<jsp:getproperty name="user" property="name">
Age :<jsp:getproperty name="user" property="age">
What will be the outpt of these two jsp:getProperty with out using any jsp:setProperty
Basically it should return default values, if iam not wrong.
Name : null
Age : 0
one of the mock exam gives the right answer as
Name : null
Age : null
Kindly Clarify me
-Sathi
[ January 02, 2003: Message edited by: Satheesh Kumar S V ]
 
Dominic Paquette
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it out on tomcat 4.0 and I got
name:
age:0
I thought that, as you say, it should of been the default values : String => null and int => 0!
I had a look at the servlet code generated by the container, here is what I saw:

I diden't find the documentation for JspRuntimeLibrary so I assume that JspRuntimeLibrary might override toString() and replace null with "" but I'm really not sure.
Sorry for the incomplete answer
Dominic
 
Thomas Kijftenbelt
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just try it out...

If you run it, you get the following response:
Name : Age :0
So you're right: getProperty returns 0 for age (and not null).
Greetings,
TK
 
Dominic Paquette
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but for the name property, should't it return null instead of an empty String?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does return null. Some JSP engines print null as the string "null", others (more sensibly) display it as an empty string.
- Peter
 
Dominic Paquette
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this type of question would probably not appear on a real exam since, the result is container-dependent, right?
Dominic
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure. The specification is a bit vague on this -- it says that for the value returned by a JavaBean property the conversion to String is done as in the println() methods. Since JspWriter.println(), like PrintStream.prinln(), will print the string "null", this is arguably what a compliant JSP container should do as well. Only, many don't, and as a JSP developer who doesn't want his users to be confronted with confusing output that's the way I'd prefer it
- Peter
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic