GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean problem ..Major one !!  Please Help !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:useBean problem ..Major one !!  Please Help !!" Watch "jsp:useBean problem ..Major one !!  Please Help !!" New topic
Author

jsp:useBean problem ..Major one !! Please Help !!

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hello friends

I am using Tomcat 5.

In servlet I wrote the following code

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
foo.mybean mb=new foo.mybean();
mb.setName("Universe");

foo.mybean mb1=new foo.mybean();
mb1.setName("World");

request.setAttribute("obj",mb1);
request.getSession().setAttribute("obj",mb);

request.getRequestDispatcher("/useBean.jsp").forward(request,response);
return;
}

Now in useBean.jsp I wrote the following code

<jsp:useBean id="obj" class="foo.mybean" scope="session"/>
<jsp:getProperty name="obj" property="name"/>

In foo.myBean I have just a getter and setter method for setting and getting the name..

Now I was expecting to get the output "Universe" but was shocked to see that I was getting "World" as output despite of me setting the scope to "session" in <jsp:useBean> tag !!

Is this a bug in Tomcat 5 or I am misunderstood ?

Please help me ..
Waiting for your replies..

Thanks and Regards
Rohit
Pushkar Raste
Greenhorn

Joined: Jan 11, 2005
Posts: 9
Hi Rohit,
Its not the problem with Tomcat. It is the JSP 2.0 spec says that <jsp:useBean>, <jsp:getProperty> and <jsp:setProperty> uses findAtrribute to find the bean.

So objects are always searched in the order page, request, seesion and application. thats why u r getting object from request scope n not from session scope.

let me know if i ma wrong

thanks
Pushkar S. Raste
[ February 25, 2005: Message edited by: Pushkar Raste ]
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
I agree with you. And I think this could be a really "good" and tricky question for a mock exam.

Cheers.
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

I'm not sure to understand fully.

Do you mean that even if you specify the scope attribute, jsp:usebean will search all scopes and that the scope attribute is only used when creating a new bean because an old one was not found ?


--
Brown belt on KnowledgeBlackBelt.com.
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

Apparently, the preceding answers seem wrong. Here is what says the JSP 2.0 specification:

The jsp:useBean action is quite flexible; its exact semantics depends on the attributes given. The basic semantic tries to find an existing object using id and scope. If the object is not found it will attempt to create the object using the other attributes.

And in the Tomcat 5.0.30 source code (org/apache/jasper/compiler/Generator.java), I found that Tomcat uses the JspContext.getAttribute(String name, int scope) and not the JspContext.findAttribute(String name). And if the scope attribute is not given by the developper, the "page" scope is used by default.

So the scope tag attribute is well used when retrieving the bean.

Rohit, maybe you should check an error somewhere else:
- Check if the name attribute of foo.mybean is not declared as static
- Check that you have no typo in your real code (the one you pasted in your question is right)
- Check that your server isn't using another version of your code (bad web-app location, outdated cache, ...)

Apart from this, I cannot imagine what has gone wrong.

I hope these informations will help you.
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
To be precise, Pushkar, the jsp:useBean action doesn't use findAttribute (I think) to find the bean. The JSP 2.0 spec says:
"The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method."

So the jsp:useBean action locates correctly the bean in the session scope (mb). But, Francois, the problem of Rohit arises when he uses the jsp:getProperty action, which uses findAttribute and so, it starts looking for the attribute in the order page, request, session and application. And, therefore, if finds first "the other" bean (mb1).

My conclusion is the following:
if there's another attribute with the same name and a more specific scope, it is not possible to get the attribute with the less specific scope using jsp:getProperty.

Please, tell me if I am wrong.
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

Jose, you are right: the getProperty indeed uses the findAttribute method. This seems to be a bug of the specification that will effectively prevent someone to get a "larger" scope attribute if a "narrower" one exists.

And there is another thing odd in the code about this tag: it uses the bean type of the bean declared with the useBean tag.

Here is an example:

Imagine you store a bean of a class foo.Person with a "firstName" property under session scope with name "foo". You then store a foo.Address bean with only a "city" property under request scope with name "foo".

Now, create a JSP with the following code:


If you try to execute it, you'll get really weird things...
(a ClassCastException !!!)

The only way I could get the info I wanted was using EL:
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Thanks for the test, Francois.

There must be a reason for the jsp:getProperty action to have this odd behavior:
"If there's another attribute with the same name and a more specific scope, it is not possible to get the attribute with the less specific scope using jsp:getProperty."

Why does it use the findAttribute method to find the bean? Why doesn't it have a "scope" attribute?

Any explanation would or supposition will be appreciated.
Pushkar Raste
Greenhorn

Joined: Jan 11, 2005
Posts: 9
Is there n e way we can get correct explanation for this, instead of making some wild guesses?

Can we report it to Sun so that we will get exact answer for this


Thanks
Pushkar S. Raste
 
GeeCON Prague 2014
 
subject: jsp:useBean problem ..Major one !! Please Help !!