File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes standard actions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "standard actions." Watch "standard actions." New topic
Author

standard actions.

Janhavi Namshikar
Ranch Hand

Joined: Sep 29, 2005
Posts: 85

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.


"Weekends don't count unless you spend them doing something completely pointless."
Manish Agarwal
Greenhorn

Joined: Jan 30, 2006
Posts: 20
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


SCWCD1.4, SCJP1.4, OCA 9i
Janhavi Namshikar
Ranch Hand

Joined: Sep 29, 2005
Posts: 85

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.
Sivasundaram Umapathy
Ranch Hand

Joined: Aug 10, 2002
Posts: 360
Hi Janhavi.

You would still get the same exception.

Regards
Siva


Siva
Co-Author - SCMAD Exam Guide - ISBN:9780070077881
Author - Java certification success, Part 4: SCEA
Janhavi Namshikar
Ranch Hand

Joined: Sep 29, 2005
Posts: 85

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.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
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.


/** Code speaks louder than words */
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: standard actions.