• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Doubt in working of jsp:useBean

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
reply
    Bookmark Topic Watch Topic
  • New Topic