This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:useBean" Watch "jsp:useBean" New topic
Author

jsp:useBean

Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
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
Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
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


SCJP1.4 (86%)
Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290
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.


SCJP | SCWCD | SCBCD | SCWSD 5 | SCEA (I) 1.4 | SCEA 5 | IBM SOA 669
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
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

Joined: Feb 23, 2006
Posts: 62
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.
parra matta
Ranch Hand

Joined: Mar 23, 2006
Posts: 40
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

Joined: Nov 11, 2005
Posts: 127
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.

Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
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

Joined: Mar 23, 2006
Posts: 40
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.
Jigar Gosar
Ranch Hand

Joined: Jul 09, 2002
Posts: 95
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 ]

Regards,<br />Jigar Gosar (SCJP 1.4)<br />visit: <a href="http://jigar.org/blog" target="_blank" rel="nofollow">http://jigar.org/blog</a>
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
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?


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
parra matta
Ranch Hand

Joined: Mar 23, 2006
Posts: 40
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

Joined: Jul 09, 2002
Posts: 95
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

Joined: Dec 04, 2004
Posts: 950
Hi,

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

Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jsp:useBean
 
Similar Threads
scope attribute (HFSJ)
doubt in page 356 of HFSJ
jsp:useBean doubt
read file replace string
jsp:useBean and default scope (head first mock exam question)