• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you. And I think this could be a really "good" and tricky question for a mock exam.

Cheers.
 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Francois Roland
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
No holds barred. And no bars holed. Except this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic