3 is correct ...but the answer states it is 2..am i right
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Which of the following staments are correct about the following jsp lines: <jps:useBean id=”name” class=”java.lang.String” /> <%= name %> 1) It won't 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 the output. 4) It will work if you create a javabean class with only one variable of type java.lang.String
OK, here is my weak area, but I do remember that it will return the value from a variable named "name", so 2 is correct. I think I read it in the specs, which states that if you have a line like <%= name %> In the two lines of <jps:useBean id=�name� class=�java.lang.String� /> <%= name %> Since no get method is called it looks for a get method in name called getName, like if you had put <%=name.getName()%> Mark
Well, I think the declaration of the bean is ok, because java.lang.String has a no argument constructor and the bean has page scope. But the container will try to find a variable named 'name' and will spew an error on not finding it. Unless the 'id' of the bean is referenced or something like 'PageContext.getAttribute(name)' is used, the bean cannot be referenced in the manner shown
Now come to think of it, I think it is a scoping problem . If you try this <jsp:useBean id="name" class="java.lang.String" > <%= name %> </jsp:useBean> or <% { %> <jsp:useBean id="name" class="java.lang.String"/> <%= name %> <% } %> it will work. So the scripting variable is obtained only within the useBean tag or in scope. It can't be obtained outside the scope.
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
Whoa, too many contradictory opinions here.Will retrieve a bean with the name "name" from the page scope. If it isn't there, the bean will be instantiated and stored in the page scope. In either case, a scripting variable "name" will be created with the bean reference in it. The variable will be available in the entire rest of the page, from the opening tag onwards. Not just between opening and closing tags.in general, a scriptlet <%= expression %> will be translated to something likeIn this case, this means that name.toString() will be called and the result included in the output stream. The correct answer is therefore indeed (2). - Peter [ March 30, 2002: Message edited by: Peter den Haan ]
Goan Balchao
Ranch Hand
Joined: Mar 25, 2002
Posts: 93
posted
0
Thanks for the clarification Peter. Sorry about my earlier posting. I misunderstood the scope for the variables introduced in this manner.
subject: 3 is correct ...but the answer states it is 2..am i right