Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Mock Exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Mock Exam question" Watch "Mock Exam question" New topic
Author

Mock Exam question

kavitha yalamanchili
Greenhorn

Joined: Oct 10, 2002
Posts: 25
<jsp:useBean id="name" class="java.lang.String"/>
<%= name %>
1) It wont compile
2) It is a valid jsp line and it will print the variable called name
3) it will compile but it will always produce null as output
4) it will work if u create a javabean class with only one variable of type java.lang.String
The given answer is 2
what about 3?
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Hi, try doing this:
<%
String s =new String();
out.print("String "+s);
%>
Something similar is happening here.Hope u got it.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
<%! String name="trupti";%>
<jsp:useBean id="name" class="java.lang.String/>
<%= name%>
I tries the above code on tomcat4 but it doesn't print anything..
Then I commented the <jsp:useBean...%>line.
and the name variable was printed on the screen..
what is exactly happening here??
If I try to print the name variable before the use of <jsp:useBean..> the name "trupti" is printed on the screen but if I try to print it after the <jsp:useBean ..> Then nothing is printed on the screen..
like this..
<%! String name="trupti";%>
<%= name%> (name printed on the screen)
<jsp:useBean id="name" class="java.lang.String"/>
<%= "second time:" +name%>(name is not printed on the screen)
what exactly is happening..
thanks,
Trupti
[ November 14, 2002: Message edited by: trupti nigam ]
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
Trupthi,
That was interesting. It is obvious from what you say is that once a variable is used in the use bean action it can't be used for printing.
How about this...
<%! String name="trupti";%>
<jsp:useBean id="<=name>" class= "java.lang.String"/>
and then trying to print it
Does this print now?<%= name%> .
I think it will.
Also, please look at my other posting about request time attributes. Thanks, vasu


What a wonderful world!
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
vasu,
I tried your code and it's giving me compilation error like..
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 2 in the jsp file: /jsp/testD.jsp
Generated servlet error:
C:\tomcat\work\Standalone\localhost\myDevelopment\jsp\testD$jsp.java:63: Missing term.
java.lang.String <%= name%> = null;
Did you try this code??
^
thanks,
Trupti
Rama Raghavan
Ranch Hand

Joined: Aug 22, 2001
Posts: 116
As Vedhas put it, what is the output when your print new String()
Also, starring at this line should answer your question:
<jsp:useBean id="name" class="java.lang.String"/>
usebean tries to get the object from the defined scope, and if it does not find it, creates new of specified class.
So <%=name%> is printing an empty string but not null ('cos name is not null).
It is not visually obvious, and hence the confusion,i'd presume..
Rama
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
Rama, good point. You hit the nail on the head.
Try adding the following code to the page:

It tells you exactly what's happening on the page.
MS
kavitha yalamanchili
Greenhorn

Joined: Oct 10, 2002
Posts: 25
Trupti,
basically the line
<%! String name="trupti";%> is at class level
and it is not specifically set into any of the valid scopes(i mean session, application..)
but the following line
<jsp:useBean id="name" class="java.lang.String"/>
is basically searching for the object name "name" belonging to the class "java.lang.String" in "page" scope. As the page scope did not have an attribute called "name" it instantiates a new string object. Thats the reason your "trupti" wasn't showing up.
I guess the important point here is scope
and yes now I understand why 3 is not right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Exam question
 
Similar Threads
mock question
doubt in <jsp:usebean..
mock exam question
JSP - UseBean
Question reagrding Beans....