File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Doubt in working of jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Doubt in working of jsp:useBean" Watch "Doubt in working of jsp:useBean" New topic
Author

Doubt in working of jsp:useBean

omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Hi all,

I am learning basics of jsp and trying out my own examples. I have a doubt with regards to working of <jsp:useBean> tag. The scenario is as follows: -

login.jsp: -


.... thus i set two objects in session 1) date and 2) user object in login.jsp ... and i try to access these objects in simplejsp.jsp using the <jsp:useBean> .... but it does not work !

simplejsp.jsp



i get an exception here saying:-



/simplejsp.jsp(15,2) The value for the useBean class attribute org.omkar.swajsp.vo.User is invalid.



.... but why only for the user object??? ... why not for the date object, as i have also mentioned, class attribute for date object??? ... i modified the code ... something like this: -



and it works! ... here i have changed the class attribute to type attribute for the <jsp:useBean> tag for user object ...and the page is displayed as:-


Sun Jul 06 11:55:28 IST 2008
org.omkar.swajsp.vo.User@d9205


.... so i did not get the working of the <jsp:useBean> and its attribute...
i tried to follow this link ... but did not understand much!

Please help me !


Thanks and Regards
Omkar Patkar (SCJP 1.4)[url]http://javacollectionsnotes.blogspot.com[/url] | [url]http://omkar-myscjpexp.blogspot.com[/url]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

If the User class does not have a default constructor, it cannot be used with the class attribute.

If you are sure that the instance will exist in the specified scope, using type is sufficient.

P.S. You are aware that putting Java code in JSPs, and particularly database code, is considered a very poor practice, right?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Thanks Bear....yes...i know in actual practise i won't use Java code in jsp...but right now i am learning the basics....so that is why you can see a good amount of java code in my jsp.

... and yes...there was no default constructor in User object. I declared one and used "class" attribute and it worked! ...MUCHAS GRACIAS ! .... but then how does it work now ? ... i mean how does "type" and "class" attribute work and they differ from each other ?

One observation i made was..... i placed a SOP in the default constructor of the User object....and when there was no session created and when i accessed the page, the SOP was printed in the console, but when session was created and when i accessed the page, it did not print any thing in SOP! .... i guess i am coming back unknowingly to my question....."how does <jsp:useBean> work ?"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

It's all spelled out in the JSP Specification. That's where you need to go for the details.

In short, type expects the scoped variable to already exist, while class will create the bean if it doesn't. That's why there needs to be a default constructor. There are more nuances that you can read about.
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
Thanks...ok i will go through those again.....but they don't have examples to explain the behavior ...that is why i had to ask here.... .... can you please suggest me some good JSP Book to start with ??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in working of jsp:useBean