| Author |
WhizLabs Question : jsp:useBean
|
Edisandro Bessa
Ranch Hand
Joined: Jan 19, 2006
Posts: 584
|
|
Hi guys,
Could you please help me out ?
Question :
What will be the result of accessing a JSP page containing this code using the URI /test/a.jsp?intValue=3
a) Prints 3
b) Prints 0
c) Does not compile
d) Throws an exception
I thought this code would not compile because Integer class does not have a default no-args constructor as required by <jsp:useBean> element.
But according to WhizLabs, this code would throw a InstantiationException.
Am I wrong ?
|
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
|
 |
Nilesh Raje
Ranch Hand
Joined: Aug 02, 2005
Posts: 153
|
|
I guess its would throw an instantiation Exception as the the class the Wrapper Interger class cannot be instantiated. This will take place at runtime so it will throw instantiation error...! So your Answer compilation Error is wrong..! [ October 28, 2006: Message edited by: Nilesh Raje ]
|
Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
|
 |
Alex Serna
Ranch Hand
Joined: Sep 18, 2009
Posts: 58
|
|
I might be wrong but to my understanding when the jsp is translated it will generate a code like
and since a no-arg constructor doesn't exist, the code won't compile. How can that cause a RunTimeException?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Here is an excerpt from JSP specification
If the class attribute is used for a Java type that cannot be instantiated as a JavaBean, the container may consider the page invalid, and is recommended to (but not required to) produce a fatal translation error at translation time, or a java.lang.InstantiationException at request time.
So the answer can both be translation time error or InstantiationException when request is received...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Alex Serna
Ranch Hand
Joined: Sep 18, 2009
Posts: 58
|
|
Thank you Ankit,
Still, to me translation error makes sense, but an Exception at request time... let's say our jsp was loaded on startup... first it would be translated and after that compiled(no request would have come yet, I think this isn't considered request time yet). IMO this wouldn't make it to the first request, it would fail at compilation time.
What do you think?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Read the excerpt from the spec again, it doesn't say an exception at the first request. What I think it means is that there will be an exception at every request to that page, not when the page is translated or at compile time. So you can catch that exception using c:catch or try-catch or use an exception page in response to the InstantiationException...
|
 |
Alex Serna
Ranch Hand
Joined: Sep 18, 2009
Posts: 58
|
|
Ok, I'll just think about it as something it will happen at request time...
Thank you Ankit!
|
 |
 |
|
|
subject: WhizLabs Question : jsp:useBean
|
|
|