Using Javabeans Components in JSP Pages

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Chapter 6

Using JavaBeans Components


in JSP Pages

What Is a Bean?

Implement a Java Bean

use a package name


A bean class must have a no-argument
constructor
The bean properties are accessed
through getter and setter methods
the bean class should implement the
java.io.Serializable or the
java.io.Externalizable interface
The property name is case-sensitive and
always starts with a lowercase letter

Declaring a Bean in a JSP


Page

Reading Bean Properties

Using the <jsp:getProperty> Action

Using the JSP Expression Language

<jsp:getProperty name="cartoon"
property="fileName" />

<img src="images/${cartoon.fileName}">

If you need to generate an image


dynamically, you should use a servlet

<img src="imageGenServlet?width=100&height=100">

Setting Bean Properties

Using the <jsp:setProperty> Action

<jsp:setProperty name="msg"
property="category" value="thoughts" />

Using the JSTL <c:set> Action

<c:set target="${msg}"
property="category" value="thoughts" />

To set a JSP action attribute to the


value produced by another action

Automatic Type
Conversions

When you use the <jsp:setProperty> or the


JSTL <c:set> action, the container takes care
automatically of the conversion from text
values to the Java types

Conversion of text value to


property type

Setup JavaBean

Setup Environment Variables

Compile JavaBean

Javac CartoonBean.java
Javac classpath servlet-api.jar MyServlet.java

Create directories in classes

path %java_home%\bin;%path%
set ClassPath %java_home%\lib

com\ora\jsp\beans\motd

Copy MyBean.class to com\ora\jsp\beans\motd


Reload Web Application

You might also like