Skip to Content
Head First Servlets and JSP, 2nd Edition
book

Head First Servlets and JSP, 2nd Edition

by Bryan Basham, Kathy Sierra, Bert Bates
March 2008
Intermediate to advanced
911 pages
20h 31m
English
O'Reilly Media, Inc.
Content preview from Head First Servlets and JSP, 2nd Edition

Adding a type attribute to <jsp:useBean>

With the changes we just made to the Person class, we’re in trouble if the attribute can’t be found:

image with no caption

Our original JSP

<jsp:useBean id="person" class="foo.Person" scope="page"/>

Has this result

java.lang.InstantiationException: foo.Person

Because the Container tries to:

new foo.Person();

Note

Person is now abstract! Obviously, you can’t make one, but the Container still tries, based on the class attribute in the tag.

We need to make the reference variable type Person, and the object an instance of class Employee. Adding a type attribute to the tag lets us do that.

Our new JSP with a type

image with no caption

Type can be a class type, abstract type, or an interface—anything that you can use as a declared reference type for the class type of the bean object. You can’t violate Java typing rules, of course. If the class type can’t be assigned to the reference type, you’re screwed. So that means that class must be a concrete implementation of the type.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Head First Java, 2nd Edition

Head First Java, 2nd Edition

Kathy Sierra, Bert Bates
Head First Java, 3rd Edition

Head First Java, 3rd Edition

Kathy Sierra, Bert Bates, Trisha Gee
Learning Java, 6th Edition

Learning Java, 6th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck

Publisher Resources

ISBN: 9780596516680Errata PageSupplemental Content