aspose file tools*
The moose likes JSP and the fly likes useBean with different scope on the same bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "useBean with different scope on the same bean?" Watch "useBean with different scope on the same bean?" New topic
Author

useBean with different scope on the same bean?

Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
A jsp uses a simple javabean(not EJB) with applicationscope(data never to change unless some admin updates db with presentationdata), and another jsp uses the same bean, BUT is only interested in accessing it's state in pagescope. Does this affect the container
managing the bean in a "wrong" way or is this approach OK? I was wondering when the thread comes to the pagescope directive, finish executing this jsp, then "kills" the bean when it goes out of scope, even though I earlier stated applicationscope on the bean?
I just can't make this one out by reading the net or books...


Steffe
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Stefan
From page 71 #5 of the JSP spec.
If a bean with the given name and in the given scope is not found a new instance will be created.
Here are the first 5 rules:

The actions performed in a jsp:useBean action are:
1. An attempt to locate an object based on the attribute values id and scope. The
inspection is done synchronized per scope namespace to avoid non-deterministic
behavior.
1. A scripting language variable of the specified type (if given) or class (if type
is not given) is defined with the given id in the current lexical scope of the
scripting language.
2. If the object is found, the variable�s value is initialized with a reference to the
located object, cast to the specified type. If the cast fails, a java.lang.ClassCastException
shall occur. This completes the processing of this jsp:useBean action.
3. If the jsp:useBean element had a non-empty body it is ignored. This completes
the processing of this jsp:useBean action.
4. If the object is not found in the specified scope and neither class nor beanName
are given, a java.lang.InstantiationException shall occur. This completes the
processing of this jsp:useBean action.
5. If the object is not found in the specified scope, and the class specified names
a non-abstract class that defines a public no-args constructor, then the class is
instantiated. The new object reference is associated with the scripting variable
and with the specified name in the specified scope using the appropriate scope
dependent association mechanism (see PageContext).

Hope that helps


Dave
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
Thankyou for the answer! But I'm still puzzled.
samplecode from my app;
<jsp:useBean id="theMenuBean" scope="appliction" class="xxx.MenuBean" />
This snippet is from a js(1) that loads uses the bean for presentation-data that seldom changes.
<jsp:useBean id="theMenuBean" scope="page" class="xxx.MenuBean" />
...and this snippet is from a jsp(2) whos only responsibility is to load a certain .gif via an include-directive in another jsp(3). jsp(2) invokes a getXXX in the MenuBean.
Maybe this explanation to my puzzle was not necessary, but if I was unclear last time, maybe this helps.
Or should I delete the "useBean with pagescope"-directive, and ask the implicit applicationobject for the menuBean?? If that is the answer, what's the code to do this?: "application.getAttribute("theMenuBean")" and THEN invoke whatever method on the bean??? How do I get the applicationobject?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Steffe
Are you getting an error doing it this way? Or strange behavior? It should work ok the way you have it and you'd get two different instances of the xxx.MenuBean class. If the second use depends on a value being previously set then it wont work as you're getting two different objects.
If the bean is already created then you can just use the same instance on multiple pages. In other words if the bean has functionality that can be applied across multiple sessions then just continue to use the same one that you created in application scope. Or, if it only has meaning in the same session then change it to have session scope, and use the same one on all of your pages. Then, regardless of the page you use the useBean tag on, if you always use the same id and scope, you'll always get the same bean instance.
The other option is to use two separate beans. From your explaination it isn't clear but it sounded as if the bean is doing two completely different jobs depending on which page it used on. If that is the case then you might want to separate out the different functionality and make two different beans.
hope that helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: useBean with different scope on the same bean?