This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean code snipets...confusion about why it does not compile.

 
Tyler Wright
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I have confusion on why code snipet2 works as it does...perhaps someone can unconfuse me

Snipet 1:

The code will compile and renders nothing in the browser: empty string.

My understanding:
The useBean action finds a no-args constructor in java.lang.String and instantiates the object...action is happy.
The setProperty action examines the object (which it hopes is a javaBean) and determines it has no method setMystring(String) and nothing happens.

Snipet 2:

This code will NOT compile: Duplicate local variable mystring

The jsp scriplet creates a String object mystring.
The useBean action attempts find mystring and finds there is already a local instance of the object.

However, I was thinking that useBean action if it finds an existing Bean (I know String is not a bean here, but its no arg constructor fooled useBean in snipet1) of the id reference, in the same scope, it will do nothing.
It will only attempt instantiation if it does not find an instance of the id="mystring".

So why does the compiler throw a compilation error?

thanks,
Tyler


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you have <%=mysyring%> instead of <%=mystring%>. I guess it's just a typo.

So why does the compiler throw a compilation error?

Because the container will instanciate the bean identified by "mystring" with a variable of the same name.
So imagine that in the generated servlet, you will have two variables called "mystring", which will cause a compilation error.

It would actually "work" if you had used a declaration like :

. Why ? Because in this case, the "mystring" variable would be an instance variable, so the useBean "mystring" variable, which is local, would shadow it. But, the useBean tag will not reuse the scriptlet variable. Things declared in scriptlets have nothing to do with scoped variables. The useBean tag will not see it, so it will not reuse the "mystring" variable from the scriptlet.

It's easier to understand if you look at the generated java code for your JSP and see where both "mystring" variables are declared.
 
vani venkat
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<% String mystring = new String("Hello"); %>


Here mystring will be declared as local variable in service method of generated servlet.

useBean id is also declared as local variable. if you have used a variable name in scriptlet to instantiate an Object(any Class for that matter, need not be same class as in useBean), it is a local variable in service method, so you can not have same name used in id of useBean.

as Christophe said, it will work if you put it in declaration, because that will be an instance variable in generated servlet.

My understanding:
The useBean action finds a no-args constructor in java.lang.String and instantiates the object...action is happy.



first id variable is declared. then using pageContext or jspContext, it looks for bean(class) in <scope> and assigns it to the variable. it does not look for mystring declared in scriptlet rather it looks for attribute in one of the four scopes.
 
Tyler Wright
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe and Vani,

I am confused on a point, let me try to explain...

jsp:useBean action instantiates or uses attributes in page, request, session, or application SCOPE.

<%! String myString = new String() %> : creates a servlet member variable.

<% String myString = new String() %> : creates an instance variable in _jspService();

OK, is scope of servlet instance the same as page scope?
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tyler,

results in a

String myString; declaration in the _jspService() method

and so does-



Multiple declarations in the same method is what that causes the compilation error.

HTH,
Vishwa
 
vani venkat
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, is scope of servlet instance the same as page scope?


instance variable will be avialable to other requests as well. if we want to find out how many times a jsp has been requested, we declare instance variable which is going to be available for other requests to the jsp using <%! .... %> I donot think it is same as page scope.


<% String myString = new String() %> : creates an instance variable in _jspService();


This is not going to create instance variable. it is going to create a local variable inside _jspService method. once service method execution is over, it is not going to be available.

 
Tyler Wright
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

All, I got it now...

The useBean action creates its bean in _jspService method...and (of course) so does <% scriplet code %>

Now I see, the useBean in the _jspService not only creates or uses objects in this method, it also accomplished its scope assignment there too....


Thanks all for your help...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic