• 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

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello

question:

is i have <jsp:useBean id class>

but i do not have "scope"

the container will search in every scope (page|request|session|context)
or it will search in the defaul scope (i.e. page) before
creating a new bean?

thanks
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I think the bean will be first searched in all the 4 scopes. If it is not present in any of the scope it will create one in page scope.

Correct me, if i am wrong.

Thank you
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

well, as far as i know...

1 - <jsp:useBean> - always PAGE SCOPE.

2 - <jsp:useBean> + BODY - If you utilize <jsp:setProperty> within <jsp:useBean> as body content, The container will utilize "pageContext.findAttribute() for updating the property value"...I mean, it will update the first attribute that it finds.


3- <jsp:getProperty> - don�t have "scope" attribute and the Container finds in all scopes.


does it make sense??

Please correct me if wrong.

Tks.
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernesto Leyva:
hello

question:

is i have <jsp:useBean id class>

but i do not have "scope"

the container will search in every scope (page|request|session|context)
or it will search in the defaul scope (i.e. page) before
creating a new bean?

thanks



If no scope attribute defined in the <jsp:useBean> the container sesrch the attribute in the page scope only before creating new bean.

For the getProperty and setProperty the contianer search the name attribute in all 4 scope in order page,request,session,application.

Thanks
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some doubts. According to what you say Narendra this should work fine, but it is not. I'm getting an exception.

In a servlet:



In a JSP:



On the other hand this works and sets new value for the property (servlet stays the same, we change JSP):




Looks like the <jsp:getProperty> didn't look in all scopes, just page scope, but the <jsp:setProperty> was able to set the new value for the property even if the attribute is in the application scope.

Would be appreciated for any justification, as I can't find the answer by myslef.
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to include something like this:

<jsp:useBean id="dog" class="foo.Dog" scope="application />

along with jsp:getProperty to have it work fine.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, according to the spec, you must use jsp:useBean to create or locate a bean before using jsp:setProperty or jsp:getPropeerty.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I'd like to point out that there's inconsistency. According to my examples the <jsp:setProperty> is looking in other scopes, and the <jsp:getProperty> is not. They behave differently.

Another thing is, we don't have to use the <jsp:useBean> to make the bean object available for use for the <jsp:getProperty>, we can do it like this and it works.

 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shall we settle this once and for all?

I've run a quick example on the 1.4 Reference Implementation (Sun AppServer 8), and I think it helps demystify everything.

JSP:

Generated Servlet:

So, as you can clearly see, as Narendra said, <jsp:useBean /> has a default scope of page (confirmed in the spec.) and <jsp:getProperty /> and <jsp:setProperty /> search all scopes for the scoped attribute.

However, you should really always use <jsp:useBean /> along with the other getter/setter actions, just in case your container generates a slightly different structure of JSP and relies on <jsp:useBean /> being invoked first. I believe it is unwise not to use <jsp:useBean />.

Aleksander, I can only suggest there is a bug in your container - what brand/version is it, and have you checked their bug tracker?
[ April 20, 2006: Message edited by: Charles Lyons ]
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charles you are always on time I was astonished to find out this behaviour, because spec says <jspProperty> and <jsp:getProperty> will refer to an object through pageContext and it's findAttribute() method. Thank you for clearing all the doubts. We need this for the exam and in fact for our future work.

As for my container, it's Tomcat 5.5.16
 
parra matta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using tomcat 5.5.12

Here is my servlet code:

Person p1 = new Person();
p1.setName("Request");

request.setAttribute("person", p1);

Person p2 = new Person();
p2.setName("Session");

request.getSession().setAttribute("person", p2);

request.getRequestDispatcher("/usebean.jsp").forward(req, resp);


Here is usebean.jsp

<jsp:useBean id="person" class="foo.Person" scope="session"/>

<jsp:getProperty name="person" property="name"/>

And the result:

Request.

Something is definately wrong in tomcat. This is because tomcat uses findAttribute and it always return the one in request scope. It just ignore the jsp:useBean.

I tested the code in weblogic 8.1, It returns Session which is what I expected.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
parra matta:

your expreiment doesnot prove that something is wrong with tomcat.

even though u used following jsp:usebean


still jsp:getProperty ...

will search for the person bean in all scopes starting from pagescope, and thus will find the bean present in the request. this is not the recomended behaviour as far as jsp 2.1 spec is conscerned, but neither is it disallowed by specs. hence nothing is wrong with tomcat.


also i want to quote a para from jsp 2.1 spec titled JSP.5.3 <jsp:getProperty>

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.
The object named by the name must have been �introduced� to the JSP
processor using either the jsp:useBean action or a custom action with an
associated VariableInfo entry for this name. If the object was not introduced in this
manner, the container implementation is recommended (but not required) to raise
a translation error, since the page implementation is in violation of the
specification.
Note � A consequence of the previous paragraph is that objects that are stored
in, say, the session by a front component are not automatically visible to jsp:set-
Property and jsp:getProperty actions in that page unless a jsp:useBean action, or
some other action, makes them visible.
If the JSP processor can ascertain that there is an alternate way guaranteed to
access the same object, it can use that information. For example it may use a
scripting variable, but it must guarantee that no intervening code has invalidated
the copy held by the scripting variable. The truth is always the value held by the
pageContext object.


[ April 22, 2006: Message edited by: Jigar Gosar ]
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, when the search is initiated by a getProperty or setProperty,

the order of search is Page, Request, Session, Application - from most restrictive scope to least. easy to remember eh?
 
parra matta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it mean that i can use jsp:getProperty without using jsp:useBean?

In my previous message, how do I print out the person attribute in session?
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as of now there is no way. I hope someone else sheds more light on this matter, "whether tomcats implementation is correct according to the specs".
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tested in Sun Java System Appliocation Server 8.1. You can use jsp:getProperty/setProperty without using jsp:useBean.

Thanks
 
What are you doing? You are supposed to be reading 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