• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

duplicated variable

 
Himai Minh
Ranch Hand
Posts: 1359
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code returns Bobby:


But this code returns duplicated local variable acct:


Why does it behave like this?
 
Marcos R Oliveira
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Himai,

In the second snipet:


translates to:

And then you have duplicated variable acct.
 
Himai Minh
Ranch Hand
Posts: 1359
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for your reply.
I posted a similar issue yesterday. Assume there is a session attribute called myAccount.
I got a duplicated local variable error. myAccount variable is duplicated.

But according to Head First or EPractice Lab, <jsp:useBean .../> first looks for a bean called myAccount.
If myAccount variable does not exist, <jsp:useBean> will create a bean and store it in a variable called myAccount.

If myAccount exists in line 1, jsp:useBean in line 2 should be able to find it and store it in myAccount variable, instead of creating a new variable.
Why it throws a duplicated local variable error?
 
Marcos R Oliveira
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Himai,

Looking at the generated java code for the <jsp:useBean .../> from line 2 of the JSP:


is possible to see that myAccount is always declared, having or not an instance of Account in the session scope. And that's also why having two or more <jsp:useBean .../> with same id generates translation time error.

But according to Head First or EPractice Lab, <jsp:useBean .../> first looks for a bean called myAccount.
If myAccount variable does not exist, <jsp:useBean> will create a bean and store it in a variable called myAccount.

Ok, but according to generated code above, if myAccount variable does not exist, <jsp:useBean> will create an instance of the bean and store it in a variable called myAccount that it will always declare.

About:
If myAccount exists in line 1, jsp:useBean in line 2 should be able to find it and store it in myAccount variable, instead of creating a new variable.

I think the more accurate would be:
If myAccount exists in line 1, jsp:useBean in line 2 should be able to find it and store it in myAccount variable, instead of creating a new instance.


Regards,
Marcos.
 
Himai Minh
Ranch Hand
Posts: 1359
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Macro, thanks for showing me the generated code.
How to see the generated code?
I am using NetBean platform. But I don't see the generated code being generated.
 
Marcos R Oliveira
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Himai, what web container are you using?
 
Marcos R Oliveira
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think there is a context menu entry "view servlet" on an jsp file. Have you tried to click on it with mouse's right button?
 
Himai Minh
Ranch Hand
Posts: 1359
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Marco,
I am using Apache tomcat 8. When I run the jsp on the latest firefox, I right click on the jsp. But I don't see the context menu.
 
Himai Minh
Ranch Hand
Posts: 1359
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Marco, I see the "view Serlvet". I right click the jsp in NetBean instead of on the browser. Now, I see the generated servlet code.
Thanks.
 
Marcos R Oliveira
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:Hi, Marco, I see the "view Serlvet". I right click the jsp in NetBean instead of on the browser. Now, I see the generated servlet code.
Thanks.


I forgot to tell you that the click was on NetBeans window. Sorry.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic