• 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

standard actions.

 
Ranch Hand
Posts: 85
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have just started with standard actions (the SCWCD prep. is going at a leisure pace )
Well, I have a question regarding standard action, <jsp:useBean> and the atributes.
If I use the useBean standard action like this :

<jsp:useBean id="myobj" type="mypackage.MyClass" scope="session" />

My understanding : an instance of type MyClass should be there in the session,and it given the local name "myobj" and it will be used in the JSP.
Question: If the object is not present in the session, would an instance be created and then set in the session? Or will an Exception be thrown?

Thanks,
Janhavi.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Janhavi,

When you specify only type attribute (without class attribute) in the <jsp:useBean> action then the object specified by "id" (myobj in your case) must be already present in the scope, if it does not present then it will throw "java.lang.InstantiationException".

So punch line is "If type is used without class attribute then bean must already exist in the scope"

Thanks,
Manish
 
Janhavi Namshikar
Ranch Hand
Posts: 85
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manish,

Thanks for the answer.
Just one more question... If I have an instance of MyClass present in the session, but its not myobj but it is say.. myobj1 or anything else (but myobj)
In that case would an Exception be thrown?

Thanks,
Janhavi.
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Janhavi.

You would still get the same exception.

Regards
Siva
 
Janhavi Namshikar
Ranch Hand
Posts: 85
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Siva,

In any JSP where I use the standard action in this way :
<jsp:useBean id="myobj" type="mypackage.MyClass" scope="session" />

I should always ensure that there is an instance - myobj, of type MyClass existing in the session scope. If not, then an Exception is thrown.

Thanks once again to both.

Janhavi.
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a small correction - if object is present but of different type than ClassCastException is raised.

From MZ's notes...


The object should have been present in the session. If so, it is given the local name wombat with WombatType. A ClassCastException may be raised if the object is of the wrong class, and an InstantiationException may be raised if the object is not defined:

<jsp:useBean id="wombat" type="my.WombatType" scope="session" />



Regards,
Shivani.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic