• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

<jsp:useBean>

 
deepak carter
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deepak carter
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain with an example
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
Java Notepad Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here type must be ABSTRACT CLASS

That is not correct. There is no restriction that the class must be abstract.
 
shivam singhal
Ranch Hand
Posts: 231
Java Notepad Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
Java Notepad Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 181
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1640
9
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic