• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp:getProperty calling bean from specific scope

 
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to get a specific bean from specific certain scope using <jsp:getProperty> ?
This question is related to : One page is putting a bean on the page scope with an id and another page using same id puts a bean on the session scope and say third page is being included and is it possible for the third page to call the specific scoped bean ? Or Is this a valid scenario, as far as compiling goes ?
looking for easy fast answers , I know i could try it ..
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would guess "yes".
- satya
PS:
When you say "id" do you mean the name of the bean or the "ID" like in XML format?
Just wanted a clarification...I was assuming more like bean "name". Thanks.
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:useBean id="name' />
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will have to try that anyways to understand....
hopefully I will update you guys...stay tuned...
- satya
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following works if you use <jsp:include page="footer.jsp" />

is this a bug
if you use <%@ include file="footer.jsp" %>
you get :

Need Expert !
[ January 15, 2002: Message edited by: ersin eser ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you get the error in the logs or the console ?
I don't seem to be getting any errors with a similar example.... with Tomcat.4.0.1 and you?
- satya
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have Tomcat.4.0.1 too
code works if I use action include but throws an compile time exception if i use directive include
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay.........I screwed up!!!
< jsp : setproperty ...../ >
Hang on I am changing that to
< jsp : setProperty ...../ >

- satya
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This would give you some info

helloBean.jsp as follows

Footer.jsp as follows

Out put is

So even if it works <jsp:setProperty> has no way of accessing more than one bean and if there are more beans <jsp:setProperty> only accesses to original one
in helloBean.jsp after including footer.jsp
You would think that this is going to set the bean which is application attribute cause I am calling the setter after included footer.jsp which declares application level bean watch the out put
next I am going to put some setters into included footer.jsp 2 c what happens
[ January 15, 2002: Message edited by: ersin eser ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ersin:
I am with you on this example. I get the same error.
Reason:
Ref: JSP Spec JSP.2.10.4, Table JSP.2-1
When we use the include directive,
< %@ include ....
the content is parsed. Since we cannot have two elements with the same ID (XML rules), we get the error.
When you use the include action (request time)
<jsp : include ......
the content is not parsed. Hence we can get around this.
Let me know what you think.
regds.
- satya
ps:
My first guess is wrong.
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we do get around it !
You can put the same named beans ( bug ? ) in different layers by jsp:includes on the same page ! but <jsp:setProperty> on the final page looks like only knows about the first bean and ignores different layers.
This goes for <jsp:getProperty> too.
Also jsp:get and jsp:set is limited since they don't address layers(implicit objects)
But then There comes the help of getAttribute setAttribute
Heh he
[ January 15, 2002: Message edited by: ersin eser ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can put the same named beans ( bug ? ) in different layers by jsp:includes on the same page

ummm...same page....
Here's what I understand:
Case 1:
One.jsp:
<jsp:useBean id="nameOne" class="....." scope="page" />
<%@ include file="Two.jsp" %>
Two.jsp:
<jsp:getProperty name="nameOne" property="someProp" />
This works. It is compiled into different classes but processed as a "single jsp page" ie; the translation of One.jsp to a Java class already includes the translation of Two.jsp file.
Hence the bean "nameOne" is in page scope.

Case 2:
One.jsp:
<jsp:useBean id="nameOne" class="....." scope="page" />
<jsp:include page="Two.jsp" />
Two.jsp:
<jsp:getProperty name="nameOne" property="someProp" />
It is compiled into a different classes and processed as different jsp pages with the output of Two.jsp added to the out stream from One.jsp. This failes since in Two.jsp, there is no bean in the page scope with the name "nameOne".
Am I close to what you think or drifting away...
Thanks.
- satya
[ January 15, 2002: Message edited by: Madhav Lakkapragada ]
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One.jsp
<jsp:useBean id="hello" class="com.HelloBean" scope="session" />
<jsp:setProperty name="hello" property="name" value="Ersin" />
Hello, <jsp:getProperty name="hello" property="name"/>!
<jsp:include page="Two.jsp" />
<jsp:setProperty name="hello" property="name" value="Satya" />
Hello, <jsp:getProperty name="hello" property="name"/>!
This is Session <%=((com.HelloBean)session.getAttribute("hello")).getName() %>
This is Application <%=((com.HelloBean)application.getAttribute("hello")).getName() %>
Two.jsp as follows
<jsp:useBean id="hello" class="com.HelloBean" scope="application" />
I come from Two.jsp

Out put is
Hello, Ersin!
I come from Two.jsp
Hello, Satya!
This is Session Satya
This is Application World // default is World in HelloBean
!!!
[ January 15, 2002: Message edited by: ersin eser ]
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi - Couple of comments:
I do not believe the translation error when using <%@ include ..... is a bug. See: JSP Specs, 2.13.2, The id Attribute - the last sentence under the 1st bullet: "Duplicate id's in the same translation unit shall result in a fatal translation error."

Food for thought about the OP: Specs, 4.3, <jsp:gerproperty> says ....... "The value of the name attribute .... will refer to an object that is obtained from the pageContext object through its findAttribute() method."
The API doc's for findAttribute() say: "Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null."

Guy
[ January 15, 2002: Message edited by: Guy Allard ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ersin:
Looks like we are talking apples verses oranges...
I am talking abt an example with page scope whereas you have an example of session/application scope.
I don't know where we are going.....
Anyways, good luck.
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ersin / Guy:

Here is how thing work when you stretch the <jsp:getProperty .... /> in various scopes.
The "ID" of the bean is the same "hello" in all the scopes (page|request|session|application)
The Bean in my example is same as what ersin used - Has a String field called "beanScope" instead of "name" used by ersin.
My conclusion: The (s)getProperty searches for the bean named "hello" first in the page then in request then in the session and finally the application scope.
(The page scope is special since using the <jsp:include ...> action will give an error, by design, saying that there is no bean "hello" in the page scope.)
You can test this by changing the scopes of the beans created in the files "two.jsp" and "three.jsp" (or one.jsp for theat matter).
one.jsp
<HTML>
<head><Title>ersin</title></head>
<body>
<jsp:useBean id="hello" class="madhav.bean1.TestBean1" scope="application" />
<jsp:setProperty name="hello" property="beanScope" value="Ersin" />
Hello, <jsp:getProperty name="hello" property="beanScope"/>!<P>
<jsp:include page="two.jsp" /><BR>
<jsp:setProperty name="hello" property="beanScope" value="Satya" />
Hello, <jsp:getProperty name="hello" property="beanScope"/>!<P>
<jsp:include page="three.jsp" /><BR>
<jsp:setProperty name="hello" property="beanScope" value="Guy" />
Hello, <jsp:getProperty name="hello" property="beanScope"/>!<BR>
<P>
This is Request <%=((madhav.bean1.TestBean1)request.getAttribute("hello")).getBeanScope() %><BR>
This is Session <%=((madhav.bean1.TestBean1)session.getAttribute("hello")).getBeanScope() %><BR>
This is Application <%=((madhav.bean1.TestBean1)application.getAttribute("hello")).getBeanScope() %><BR>
</body></html>

two.jsp
=========
<jsp:useBean id="hello" class="madhav.bean1.TestBean1" scope="request" />
I come from two.jsp<BR>
three.jsp
==========
<jsp:useBean id="hello" class="madhav.bean1.TestBean1" scope="session" />
I come from three.jsp<BR>

The output from Tomcat with this combination is:
Hello, Ersin!
I come from Two.jsp
Hello, Satya!
I come from three.jsp
Hello, Guy!

This is Request Guy
This is Session notSet
This is Application Ersin

For what its worth...........
- satya
ps:
notSet is the Default value for the property.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thought I will post the code for the bean as well......

You will see the above output when you load the application the first time. Then if you re-load, then behavior is different. Restart your app server to verify. If you are interested, I can explain why?
regds.
- satya
 
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhav,
Your analysis gives a clear idea and looks perfectly valid.
Ramdhan YK
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Madhav,
This was a very informative post for me.
I wish these were kind of things that they asked @ the exam, javadoc memorization sux, and how about what xml tag nested under what type of questios... annoying. How is your studies ?
I start playing around with JWeb+ : I thought my english was bad :roll: I did standard test 5 4 3 got 78 78 75 , Long way to go...
regards
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what xml tag nested under what type
Nested tags (both xml and custom tags) are now my weak point....Thought of asking for an example from someone, but then it would be too selfish of me to ask so much...
How is your studies ?
going and going and going.......(Do you have the bunny inside.... )
I start playing around with JWeb+ : I thought my english was bad I did standard test 5 4 3 got 78 78 75 , Long way to go...
I don't have JWeb+. I have the Whiz one. Haven't tried it yet, maybe this weekend. I am not too kean on rushing it. I am planning for a March/April as I already said so, for now I am . But then, that March could become July in which case I would be ........
- satya
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ersin & Madhav, ur discussion was also very useful for me and following that i created this two jsp pages that may clarify further how bean properties change from page to page & when default values are taken. I m proving the source code here :
BeanColor.java
==============

BeanUse.jsp
===========

nextPage.jsp
=============

footer.jsp
==========

Hope it adds flavor to the discussion of Beans in JSP....
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just realized this point from the JSP SPec JSP.2.8.2 Objects and Scopes.....


A name should refer to a unique object at all points in the execution, that is all the different scopes really should behave as a single name space. A JSP container implementation may or may not enforce this rule explicitly due to performance.


So from this we should agree that my conclusion is limited to Tomcat 4.0. It maybe different in other servers.
regds.
- satya
 
reply
    Bookmark Topic Watch Topic
  • New Topic