Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
13.1 Basic Bean Use
289
This usually means instantiate an object of the class specified by
Class
,
and bind it to a variable with the name specified by
id
. So, for example, the
JSP action
can normally be thought of as equivalent to the scriptlet
<% coreservlets.Book book1 = new coreservlets.Book(); %>
Although it is convenient to think of
jsp:useBean
as being equivalent to
building an object,
jsp:useBean
has additional options that make it more
powerful. As we'll see in Section 13.4 (Sharing Beans), you can specify a
scope
attribute that makes the bean associated with more than just the cur
rent page. If beans can be shared, it is useful to obtain references to existing
beans, so the
jsp:useBean
action specifies that a new object is instantiated
only if there is no existing one with the same
id
and
scope
.
Rather than using the
class
attribute, you are permitted to use
beanName
instead. The difference is that
beanName
can refer either to a class or to a file
containing a serialized bean object. The value of the
beanName
attribute is
passed to the
instantiate
method of
java.beans.Bean
.
In most cases, you want the local variable to have the same type as the
object being created. In a few cases, however, you might want the variable to
be declared to have a type that is a superclass of the actual bean type or is an
interface that the bean implements. Use the
type
attribute to control this, as
in the following example:
This use results in code similar to the following being inserted into the
_jspService
method:
Runnable thread1 = new MyClass();
Note that since
jsp:useBean
uses XML syntax, the format differs in three
ways from HTML syntax: the attribute names are case sensitive, either single
or double quotes can be used (but one or the other must be used), and the
end of the tag is marked with
/>
, not just
>
. The first two syntactic differ
ences apply to all JSP elements that look like
jsp:xxx
. The third difference
applies unless the element is a container with a separate start and end tag.
Core Warning
Syntax for
jsp:xxx
elements differs in three ways from HTML syntax:
attribute names are case sensitive, you must enclose the value in single or
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved