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

<jsp:include> and <jsp:useBean>

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code written by me while studying the use of java beans with <jsp:include>. I have a java beab name AddressBean1 which has a property "street".

MyBean.jsp is:




second.jsp is :



when i access the MyBean.jsp i get the response:

second page
secondvalue

this is because both pages share the request scope attributes , so the first javabean object created is referenced in teh second page and its property street is overridden to the secondvalue.

Now if i change the scope in the second page to "page" that is in the second.jsp, i write:

<jsp:useBean id="myBean" class="info.AddressBean1" scope="page" />

and the whole code remains the same, then i get :

second page
firstvalue

this is because in second.jsp the bean is created in its page scope which is not shared, so when the control returns to MyBean.jsp page , it accesses its bean which is in requestscope, so it gives firstvalue.

now if i change the same line in second.jsp to either

<jsp:useBean id="myBean" class="info.AddressBean1" scope="application" />

or

<jsp:useBean id="myBean" class="info.AddressBean1" scope="session" />

i get the result:

second page
secondvalue

which i do not understand why. According to my understanding, the bean created in either session or application is not shared between the two pages ( only the request scope attributes are shared in case of dynamic inclusion <jsp:include>) so when the control goes back to MyBean.jsp, it should access the bean in its request scope and its value of street property "firstvalue".

Can anyone explain this behavior of jsp:useBean and jsp:include and correct me where i am making a mistake??





 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bean created in either session or application scope can be shared by other pages. Bean in application scope can be accessed from anywhere in the application and bean in session scope can be accessed in any page during the same session.
When you re declare the bean in second.jsp it reset the scope of the bean defined in first jsp. Hence you got the value defined in second jsp.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Harpreet, your explanation really helped me in clarifying the concept of application and session scopes sharing. I was making a blunder in that. But I do not agree with this statement of yours:

When you re declare the bean in second.jsp it reset the scope of the bean defined in first jsp. Hence you got the value defined in second jsp.



well after doing a many changes to my code i came to the conclusion (which I am not sure I am correct or not ) that when there is an including and an included page we can declare beans with the same id and different scopes without getting an error of duplicate bean name, and surprisingly, the second one does not replace the first one , neither does it reset the scope of the first one . Both exist and we can access both of them.

The jsp:getProperty and jsp:setProperty in either page operate on the bean in the most restricted scope, starting with page,request,sesssion and application.This is something strange for me as I have not read this rule anywhere in books.

well my modified code is as follows:

MyBean.jsp is



second.jsp is :




the result that i get is :

second page
req- secondvalue sess-GreenSpring Ave
secondvalue secondvalue GreenSpring Ave



Here "Greenspring Ave" is the default value of street property that i have set in the constructor of AddressBean1. Here in the second.jsp the <jsp:setProperty> sets the property street of the bean in the request scope and the <jsp:getProperty> in the MyBean.jsp also retrieves the street property of the bean in the request scope. the bean in the session scope has its default value of street set in the constructor and both exist.

Any one who knows about this , please confirm my conclusion or point out if I am wrong somewhere.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saima,

Your understanding

The jsp:getProperty and jsp:setProperty in either page operate on the bean in the most restricted scope, starting with page,request,sesssion and application.

is correct. The basic reason for this behavior can be known if you observe how the container translates both .jsp files into their correspoding servlet (.java) files. You may find them in:

yourTomcatHomeDir/work/Catalina/yourServerName/yourWebAppName/org/apache/jsp

It replaces the <jsp:setProperty/> or <jsp:getProperty/> by findAttribute() method, and as you might be aware findAttribute() starts looking from most restricted to least restricted scope, & thus the above behavior makes sense.

Moral:
1) To distinguish between the beans in different scopes, make the "id" attribute distinct in the <jsp:useBean/> standard action. OR
2) Use getAttribute(String, int) method to explicitly specify the scope in which you want the container to look for the bean (this is not a good approach though as this takes you away from using standard actions)

Hope this helps.

Prashant
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,
Interesting topic, thanks for sharing.
Regards,
Frits
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi saima kanwal

Thanks for correcting me.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prashant.
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic