Forums Register Login

jsp:useBean

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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".
+Pie Number of slices to send: Send
Hi,

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

Thanks
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1564 times.
Similar Threads
jsp:useBean and default scope (head first mock exam question)
read file replace string
jsp:useBean doubt
doubt in page 356 of HFSJ
scope attribute (HFSJ)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:03:07.