File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question on jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question on jsp:useBean" Watch "Question on jsp:useBean" New topic
Author

Question on jsp:useBean

Satheesh
Greenhorn

Joined: Jan 02, 2003
Posts: 1
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

Joined: Dec 13, 2002
Posts: 64
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

Joined: Feb 13, 2002
Posts: 73
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

Joined: Dec 13, 2002
Posts: 64
Yes but for the name property, should't it return null instead of an empty String?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Dec 13, 2002
Posts: 64
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

Joined: Apr 20, 2000
Posts: 3252
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on jsp:useBean