This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes <jsp:useBean> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "<jsp:useBean>" Watch "<jsp:useBean>" New topic
Author

<jsp:useBean>

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
Hi all,

can anyone please explain me this statement-"<jsp:useBean> can also CREATE a bean!"

if anyone can explain with a example then it was great help .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60811
    
  65

It's not used much in modern JSP. But if the class attribute is specified, and no bean of the class with the specified id exist as a scoped variable, one will be created.

Modern JSPs rely upon their controllers to create the scoped variables for the most part.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
can you please explain with an example
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60811
    
  65

Easy to find: jsp usebean examples

Secondly, it's not really something you should be doing in modern JSP pages. You aren't still putting Java code in your pages, are you?
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 223

<jsp:useBean id="shivam" class="com.example.ssb" type="com.example.ssa" scope="session" />

here type must be ABSTRACT CLASS

{
com.example.ssa shivam = null ;
shivam = (com.example.ssb) session.getAttribute("here the oject name that you have send to this JSP page");
if(shivam == null){
com.example.ssa shivam = new com.example.ssb();
}
}

this is the code inside useBean tag by spec vendors..
Here Scope is OPTIONAL , by Deafult its Page Scope
Id defines the NAME of the bean..
CLASS defines the class whose bean you want to create.

you may use SetProperty inside the useBean tag's BODY to set property of the bean.,,, but it will only RUN if you DON't HAVE SEND ANY BEAN TO JSP ..

With Regards
SHIVAM SINGHAL
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60811
    
  65

here type must be ABSTRACT CLASS

That is not correct. There is no restriction that the class must be abstract.
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 223

bear bibeault : but type MUST not be used to create bean..

TYPE IS ONLY FOR PROVIDING REFRENCE is not it??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60811
    
  65

Please read KeepItDown and avoid posting in all uppercase.

Yes, the type attribute cannot be used if the bean is to be automatically created when the named scope variable does not already exist. But that does not mean that the specified class needs to be abstract.
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 223

okey..

and class attribute must not be an abstract class.. as its bean should be created. . .
and okeys type can not always be abstract..
Arun Giridhar
Ranch Hand

Joined: Mar 10, 2012
Posts: 146

shivam singhal wrote:bear bibeault : but type MUST not be used to create bean..

TYPE IS ONLY FOR PROVIDING REFRENCE is not it??

Sorry to interrupt , type can be an Interface or a Abstract Class or a Class.


hate Professionalism
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Concrete example of Type vs Class

Type: java.util.List
Class: java.util.ArrayList

<jsp:useBean id="myList" type="java.util.List" class="java.util.ArrayList"/>

equivalent in java code to:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60811
    
  65

Good example.

But remember that, when it comes down to it, useBean isn't used much in web applications that adhere to modern standards where:
  • Beans and other data are created in the controller rather than in the JSP
  • There is no Java code in the JSP, hence no need for useBean to create scripting variables
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: <jsp:useBean>
     
    Similar Threads
    Inheritance
    What is the difference between war, jar & ear
    beanName attribute in jsp:usebean
    On jsp:useBean
    JspFragment