Chapter 23. Component-Based JavaServer Pages


Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Part V:  Web Technologies

You will learn about the following in this chapter:

  • The purpose of JSP actions

  • How to include other JSPs in your page

  • How to access JavaBeans from your JSP

  • How to use JavaBeans to access databases

JavaServer Pages (JSP) simplify the task of creating HTML-based Web pages programmatically. They enable you to create scriptlets that give you far more control over the appearance of the output than you can achieve using HTML alone, and with far less effort than using servlets.

It is possible to carry the concept of embedding Java code in scriptlets too far, however. JSP excels in providing control over the presentation of data on a Web page. If you insert too much nonpresentation logic on the page, however, it can become just as unmanageable as a huge servlet. Because of this, many system architects forbid the use of nondisplay logic in JSP. The existence of component-based development makes it possible to segregate display logic from business logic.

There are three ways to include external logic in your JSPs. You can

  • Use the include directive to insert other JSPs into your page.

  • Use the include action to include other servlets, JSPs, and HTML files into your JSPs.

  • Create JavaBeans that contain the logic that you want to use and call them from the JSP.

Component-based development is a concept that is as old as object-oriented programming itself. The basic idea is to separate your program into three or more layers or tiers.

The presentation layer concerns itself with how information looks in a browser. What color the background is? What fonts will be displayed? Will navigation be with hyperlinks or buttons? Will there be cute little animations on the page? This layer doesn't care about the contents, just the look.

The business layer determines what data will be shown. If pricing differs based on volume, distance from the factory, date of delivery, and so on, this layer will be responsible for determining the correct information to display. This layer doesn't care anything about the look of the display, just the contents. There may be several of these layers in a complex system.

The data layer is where raw data is stored and retrieved. Normally, this layer is implemented as a database. A table of prices based on distance from the factory might be stored in the database. In addition, customer data that could be used to determine the distance from the nearest factory might be stored in another table. However, the logic that uses this data to determine the actual price to display is not the business of this layer. It lives only to store and retrieve data. Figure 23.1 shows this n-tiered architecture:

Figure 23.1. The JSP provides the presentation of the data that it is handed by the business-logic layer.




The JavaBeans in the business logic layer could be Enterprise JavaBeans (EJB). There are real benefits to this approach, but a full discussion of them is beyond the scope of this book. Please see Sams Teach Yourself EJB in 21 Days from Sams Publishing for a complete discussion of EJB.

JSP calling components such as JavaBeans fit this n-tiered model very nicely. Normally, the JSP determines appearance. They call JavaBeans to request the data to display. The JavaBean calls the database to obtain the data that it needs to calculate the data to display.

In this chapter, you will learn how to add components to your JSP. We start by using the include directive. Following that, you will learn how to use JSP actions to do a more sophisticated "including" of other components such as servlets and other JSPs. Finally, you will learn how to create JavaBeans that provide information to the JSP for display. Some of these JavaBeans will get their data from databases, showing you how an n-tiered application can be built using JSPs.


    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: