Java Reference
In-Depth Information
9.return bookTitle;
10.}
11.
12.public void setBookTitle(String bookTitle) {
13.this.bookTitle = bookTitle;
14.}
15.
16.public Author getAuthor() {
17.return author;
18.}
19.
20.public void setAuthor(Author author) {
21.this.author = author;
22.}
23.
24.}
The goal of the application is to show how to access the property of an attribute (the
bookTitle
property
of
Book
in Figure
3-5
) and the property of an attribute that is itself a property of the attribute (the
name
property of
Author
in Figure
3-5
). In Figure
3-5
,you need to output the value of the
name
property of
the
Author
. It is not possible to do this using JSP standard actions, as you will see later in this section,
and in such cases, scriptlets have been used. This is how scriptlets found their way into JSP. However,
you should not use scriptlets because with the scriptlets (Java code) intermingled in the presentation
code (JSP); the business logic cross-cuts the presentation concern, resulting in unmaintainable JSP, as
explained earlier in Listing 3-4. Because JSP standard actions cannot access properties of an attribute
that is itself a property of an attribute and because using scriptlets results in unmaintainable JSPs, you
should use EL. In this section, you will learn how to use EL with the help of an example where
Author
class is the property of the
Book
class. Figure
3-5
shows the relationship between
Book
and
Author
.
You will access the
name
property of
Author
from
Book
without using scriptlets.
Listing 3-9 illustrates the
Author
object with a single property called
name
with getters and setters.
You need to output the value of the
name
property of
Author
.
Listing 3-10 illustrates the
Book
object with two properties,
bookTitle
and
author
, and their getters
and setters. The
author
property in
Book
is the
Author
class, illustrated earlier in Listing 3-9. You
need to access the
name
property of the
author
property.
The
Book
and
Author
objects serve as the model of the MVC application. Listing 3-11 illustratesthe
controller of the application.
Listing 3-11. BookController.java
1.package com.apress.chapter03.controller;
2.
3.import java.io.IOException;
4.
5.import javax.servlet.RequestDispatcher;
6.import javax.servlet.ServletException;
7.import javax.servlet.http.HttpServlet;
8.import javax.servlet.http.HttpServletRequest;
9.import javax.servlet.http.HttpServletResponse;
10.