| Author |
JSP Error
|
Freddy Wong
Ranch Hand
Joined: Sep 11, 2006
Posts: 959
|
|
Problem: Which of the given statements are correct regarding the following JSP page code? <jsp:useBean id="mystring" class="java.lang.String" /> <jsp:setProperty name="mystring" property="*" /> <%=mystring%> Assume that the request for this page contains a parameter mystring=hello. Answer: 1. It will print "". 2. It will print "hello" 3. It will not compile. 4. It will throw exception at runtime. The answer is 1. For me, I think the answer is 4 because there is no property named myString in java.lang.String. This will cause the compilation to fail after the translation of JSP page into servlet class and the container will throw an exception. Can somebody explain? Thanks. [ February 23, 2007: Message edited by: Freddy Wong ]
|
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
No, no exception. The container will look for setMyString in the String class, but if does not find it, it will skip that parameter.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Freddy Wong
Ranch Hand
Joined: Sep 11, 2006
Posts: 959
|
|
O yeah, you're right. Thanks heaps
|
 |
Hunny Lee
Greenhorn
Joined: Feb 17, 2007
Posts: 23
|
|
The answer is actually #1. -this looks for a "mystring" attribute on page scope, which should be of type String. If it does not exist, the container will create it. At this point, mystring is "". -this tries to set the properties of String, if the properties exist. The container iterates over the request parameters, and looks for anything that matches the bean's property names. Since String doesn't have properties anyway, nothing happens. <%=mystring%> -so this prints an empty String. Line 2 would throw an exception, something like: "Can't find information on property 'notARealProperty' in bean 'java.lang.String'" only if you specify the property, e.g.
|
 |
 |
|
|
subject: JSP Error
|
|
|