File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Scope of jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Scope of jsp:useBean" Watch "Scope of jsp:useBean" New topic
Author

Scope of jsp:useBean

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hello sir,madam

<%@ page import="businessData.visitorAddresses.*"%>

<%{%>

<jsp:useBean id="abc" class="businessData.visitorAddresses.AddressBean" scope="session">
<jsp:setProperty name="abc" property="name" value="Ram"/>
<jsp:setProperty name="abc" property="city" value="Pune"/>
<jsp:setProperty name="abc" property="state" value="MH"/>
</jsp:useBean>

<%}%>

<jsp:useBean id="abc" class="businessData.visitorAddresses.AddressBean" scope="page"/>

<jsp:getProperty name="abc" property="name"/>  <b> : </b>
<jsp:getProperty name="abc" property="city"/>  <b> : </b>
<jsp:getProperty name="abc" property="state"/>

In Tomcat I get the following error
org.apache.jasper.JasperException: /useBean.jsp(13,0) jsp.error.useBean.duplicate

Now even if I declare the useBean in a local block still I dont understand how it gives me error if I again write the useBean tag with same id attribute ? As the scope of id attribute is over as soon as the local block is closed.

Is this the bug in Tomcat ?

Waiting for your reply/suggestions
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I believe the main intent of useBean, as well as EL and JSTL, is to provide a higher a level abstraction that allows JSP developers to move from scriptlets to scriptless JSPs.

In this and the two other threads that you started today, you're using the useBean tag but also mixing in scriptlet code that undermines or clashes with what useBean is trying to do, and then asking if the problem is a bug in Tomcat.

If you want to implement lower level Java coding in your JSPs, why not forgo the useBean tag altogether and go with 100% scriptlet code?

Better yet, move all of your complicated Java code into servlets and beans.
Then use let the JSPs handle nothing but the simple view tasks.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
pankaj patil
Ranch Hand

Joined: Dec 19, 2004
Posts: 98
you cant create 2 instance of the class with th same name

because the object is created in the server so only one instance of the class is allowed .

you can create 2 intance of the class with different name


Regards,
Pankaj Patil
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Originally posted by Ben Souther:
I believe the main intent of useBean, as well as EL and JSTL, is to provide a higher a level abstraction that allows JSP developers to move from scriptlets to scriptless JSPs.

In this and the two other threads that you started today, you're using the useBean tag but also mixing in scriptlet code that undermines or clashes with what useBean is trying to do, and then asking if the problem is a bug in Tomcat.

If you want to implement lower level Java coding in your JSPs, why not forgo the useBean tag altogether and go with 100% scriptlet code?

Better yet, move all of your complicated Java code into servlets and beans.
Then use let the JSPs handle nothing but the simple view tasks.


I am not doing anything special. I am just trying out the example given out by jsp specification. And I guess this a geniune doubt. You can visit the specification link which you have.They have given the similar example and said that this is allowed and valid. The same I tried out in tomcat and it didnt worked.I guess there is no point in getting angry !!
And ofcourse this just tests how much thorough we are with the useBean tag.
[ November 25, 2005: Message edited by: Rohit Bhagwat ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Straight from the JSP Specification (section JSP.5.1):

Duplicate id�s found in the same translation unit shall result in a fatal translation error.


So regardless of what weird scoping contortions you place in the page, it is illegal to use the same id for more than one useBean action.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rohit Bhagwat:

I am not doing anything special.


No but, as I mentioned, in every case you are using useBean and then adding scriptlet code that clashses with what useBean is trying to do.


I am just trying out the example given out by jsp specification. And I guess this a geniune doubt. You can visit the specification link which you have.
They have given the similar example and said that this is allowed and valid.
The same I tried out in tomcat and it didnt worked.

To which example are you refering?


I guess there is no point in getting angry !!

Certainly not. We're talking about JSP code, not life and death.



And ofcourse this just tests how much thorough we are with the useBean tag.

I'm not sure what you mean by this.

I've used the useBean tag a lot over the years and never had a problem with it. In my case I'm always using it once, at the top of the page, for each bean. Whenever I refer to a class with either the type or the class attribute, I fully qualify it.
My pages also don't have any scriptlet code.

Out of curiosity, are these real world cases or are you just pushing useBean to see how robust it is?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Just as a helpful hint for future posts: claiming that you've found a bug in Tomcat every time you come across something you do not understand is a good way to get yourself onto people's "ignore list". This is known as "crying wolf" and impairs your credibility.
[ November 26, 2005: Message edited by: Bear Bibeault ]
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Bear. I appologize for using the word "Tomcat bug". Henceforth I will take care that I dont use such words.

Originally posted by Bear Bibeault:
Straight from the JSP Specification (section JSP.5.1):
Duplicate id�s found in the same translation unit shall result in a fatal translation error.
So regardless of what weird scoping contortions you place in the page, it is illegal to use the same id for more than one useBean action.


I agree with everyone that duplicate id�s found in the same translation unit shall result in a fatal translation error. This is the rule of jsp however dont you think that this violates the basic fundamentals of java that the life of a variable is limited to the scope/block in which it is declared ?.

Also Ben if you refer to page no 1-101 in the Part I-Standard Actions-JSP.5.1<jsp:useBean> section of specification.pdf file. Then it also demonstrates the same issue which I am talking. Or else following is the code straight from the specification
<% { // introduce a new block %>
...
<jsp:useBean id=�customer� class=�com.myco.Customer� />
<%
/*
* the tag above creates or obtains the Customer Bean
* reference, associates it with the name �customer� in the
* PageContext, and declares a Java programming language
* variable of the same name initialized to the object reference
* in this block�s scope.
*/
%>
...
<%= customer.getName(); %>
...
<% } // close the block %>
<%
// the variable customer is out of scope now but
// the object is still valid (and accessible via pageContext)
%>
Now since the variable is now out of scope cant I again redeclare the variable ? So was just again trying to use the useBean tag.
Although this may not be used in practical life , or you can say it is a bad practice but my question is whether this is valid or invalid rather than good or bad practices.What I mean to say by valid or invalid is -specification says that id attribute of useBean tag declared in a block (as above) is only visible to that block and not outside the block directly.

This is my last post for this topic and I wont do any posts on this topic further as I guess people have started hateing me.
[ November 26, 2005: Message edited by: Rohit Bhagwat ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Originally posted by Rohit Bhagwat:
Bear. I appologize for using the word "Tomcat bug". Henceforth I will take care that I dont use such words.


You miss the point of my post. My point is that if you overuse such a claim, it loses its impact.


This is the rule of jsp however dont you think that this violates the basic fundamentals of java that the life of a variable is limited to the scope/block in which it is declared ?.


You can't really compare scoped variables in a JSP with variables in Java. They aren't the same concept and they have very different rules.


This is my last post for this topic and I wont do any posts on this topic further as I guess people have started hateing me.


Where on earth are you getting that idea? Our suggestions are to try and help you. As Ben pointed out, it's only Java and JSP -- hardly a life or death subject!
[ November 26, 2005: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410


<%
// the variable customer is out of scope now but
// the object is still valid (and accessible via pageContext)
%>


Look at the line just under the the one that you highlighted with bold tags.
"The object is still valid (and accessible via pageContext)."

This is certainly different from typical Java behavior.
It shouldn't be surprising that an attempt to use the same ID in another useBean tag will cause an error.


Again, are these real world examples?
Are you trying to do something that's not working because useBean isn't doing what you would expect? If so, there may be other approaches that will result in clearer, more robust code.


On the "Is this a bug issue".
One nice thing about Tomcat (or any very popular open source project) is that tens of thousands of people are using it. In this environment, bugs tend to show up fast. If you think you may have discovered one, it's easy to search their bug database to see if someone else has already reported it.

This one shows the open bugs.
http://issues.apache.org/bugzilla/buglist.cgi?query_format=specific&order=relevance+desc&bug_status=__open__&product=Tomcat+5&content=

If I were looking to see if an issue I was having was a bug, I would also add "Invalid" and "Won't Fix" to the status choices. You may find out that someone else had a similar problem, reached the same conclusion that you did, but found out after entering the bug that the problem was in their code. This would be very helpful information.

If nobody else has reported it, I would take a long hard look at what I was trying to do to determine if the problem is with my code. This would include a thorough reading of the specs.

After doing all of that, I would inquire on the product's user list (if they have one). If nobody there, can tell me what I'm doing wrong or explain the behavior that I'm seeing, then I would submit the bug to the developers of that product.

Warning: The guys on the committer's list at Tomcat are very sharp and knowledgeable people but they are not known for their patience.
They can be down right "un ranch like" at times.
You'll need to have thick skin if you are going to enter that realm.
[ November 26, 2005: Message edited by: Ben Souther ]
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Rohit Bhagwat:
This is my last post for this topic and I wont do any posts on this topic further as I guess people have started hateing me.


Come on Rohit, ease up!
No one is hating you...they are just trying to help.

Bear and Ben both have been 'extremely' helpful to everyone on this forum. In fact it is a wonder how they spare time to post so much, may be there is several of them posting under those names


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scope of jsp:useBean