Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP Error

 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, no exception. The container will look for setMyString in the String class, but if does not find it, it will skip that parameter.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O yeah, you're right. Thanks heaps
 
Hunny Lee
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic