Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WhizLabs Question : jsp:useBean

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll just think about it as something it will happen at request time...

Thank you Ankit!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic