Pro JSP by Matt Raible, Sing Li, Dave Johnson, Daniel Jepp |, Paperback | Barnes & Noble
Pro JSP

Pro JSP

by Matt Raible, Sing Li, Dave Johnson, Daniel Jepp
     
 

Simpler, faster, easier dynamic website development based on new additions to an established and proven technologythat's what JavaServer Pages (JSP) 2.0 is all about. Pro JSP, Third Edition is the most comprehensive guide and reference to JSP 2.0 yet. It equips you with the tools, techniques, and understanding you need to develop web applications with JSP

Overview

Simpler, faster, easier dynamic website development based on new additions to an established and proven technologythat's what JavaServer Pages (JSP) 2.0 is all about. Pro JSP, Third Edition is the most comprehensive guide and reference to JSP 2.0 yet. It equips you with the tools, techniques, and understanding you need to develop web applications with JSP and Java servlets.

The features of the JSP 2.0 and Servlet 2.4 specifications make developing web applications easier than ever before. JSP Expression Language (EL) provides asimple language for creating JSP pages and tags. In addition, by also using the JSP Standard Tag Library (JSTL), you'll never have to use a Java scriptlet or write spaghetti code again.

Beyond covering the JSP and Servlet APIs, this book shows you how to choose and implement the best persistence option for your web applications; how to secure your web sites against malicious attack and accidental misuse; how to improve the performance and scalability of your JSP pages; and how to architect and design your applications to be reliable, stable, and maintainable through the use of design patterns and best practices.

Finally, no JSP book would be complete today without looking at the role that open source projects such as Ant, Struts, XDoclet, JUnit, and Cactus can play in making your web development even easier.

Product Details

ISBN-13:
9781590592250
Publisher:
Apress
Publication date:
09/17/2003
Edition description:
3rd ed. 2003
Pages:
624
Product dimensions:
7.00(w) x 9.25(h) x 1.27(d)

Read an Excerpt

Chapter 6: Combining Servlets, JSP, and JavaBeans

As shown in Chapter 3, the use of Servlets allowed convenient management of events and program flow, but cumbersome ability to generate responses. Conversely in Chapter 4, the use of JSP provided excellent definition of response pages but gave maintenance concerns with scriptlets embedded in between HTML. Chapter 5 introduced beans as a way of eliminating some of that scriptlet code with easy yet powerful tags.

This chapter attempts to unite the three technologies (Servlets, JSP, and beans) by blending the best of all three approaches. This approach solves most of the above problems. When we add custom tags in later chapters, we will complete the picture.

In this chapter we will:

  • Introduce the popular Model-View-Controller (MVC) architecture. In the development of Graphical User Interfaces, the MVC design pattern has emerged as a popular architecture for partitioning functionality.

  • Explore the MVC architecture and apply it to our time entry system. This useful but limited example will also dramatize the importance of comprehensive frameworks such as Struts, which is discussed in depth in Chapter 21.

  • Take a look at some advanced topics with Servlets and JSPs, such as HTTP Session Binding Events.

  • Discuss some advanced techniques for combining Servlets and JSP, including the use of event listeners.

The Model View Controller Architecture

Originating with Smalltalk designs, the MVC pattern partitions functionality into three interacting components – the Model, the View, and the Controller. You may ask why do you care? Often it is not always apparent where in your design functionality should reside. For example, is a piece of functionality better served using a JSP, Servlet, or a bean? Writing out all of your HTML from a bean is surely not the appropriate solution, just as putting all your business logic in a JSP is not appropriate.

MVC is a design paradigm where each component easily and naturally maps to our three main implementation technologies – beans, JSP, and Servlets.

The Components of an MVC Architecture

At a simple level, the components of MVC architecture interact as shown below. The Model holds the data, the View retrieves the data and generates a dynamic display, and the Controller provides the logic processing layer and delegation to the Model and View...

...Let's now look at each of the major components of the MVC design pattern in turn. We'll look at what the component provides, and how it can be organized.

The Model

The Model represents the business logic of an application. Encapsulating business rules into components facilitates testing, improves quality, and promotes reuse.

The Model can be further partitioned into State and Action components.

State Components

The State defines the current set of values of the Model and includes methods to change those values. These methods are where some of the business logic is captured.

The State components are usually protocol independent. JavaBeans are a logical choice for implementing the State components.

The reusable nature of beans allows for the somewhat independent construction of the State components. As far as being protocol independent, State components should be isolated enough so they can be accessed by applications that use HTTP, RMI, etc., that is, the protocol would be another layer on top of the component. Their construction should take into account current requirements and consider future growth and evolution. This independent construction facilitates sound design in these ways:

  • Reuse
    Elimination of presentation logic allows different applications (for different audiences or using different technology) to make use of the same business logic.

  • Quality
    By putting the business logic in one place, it can be reviewed and tested more thoroughly. Contrast this approach with the cost and less stringent test coverage if the business logic were embedded in each application that needs it.

  • Robustness
    The business logic is more easily enforced. Encapsulating the logic in one place can encourage its reuse, reducing room for errors to creep into the logic. For example, if every designer of a series of related applications (say time entry, expense reports, project budget requests, and salary planning) used the same Approval bean to route cost-related items (such as a time sheet charges to project budgets, expense reports for business trips, funding requests for pilot projects, and raise requests), the same business logic would be enforced across the entire suite of applications.
Action Components

The Actions define the allowable changes to the State in response to events. Business logic also dictates the construction of Action components.

In implementing the Action components, the choices get more complex. In simple systems, the Actions may actually get absorbed into the Controller, but this is generally not recommended. Typically a layer of Action beans is created to capture the requirements that govern interaction with the State components. As far as our time entry system, the 'Summary' action represents an example of an Action component.

Often Action components must be aware of the protocol in order to obtain information about the event. This is a dangerous situation, as it ties business logic to a specific protocol, limiting potential reuse.

Some simple rules of thumb are helpful when constructing your Action components. Adapt and add to this list for your particular set of requirements:

  • Take a passive approach to Action components, only handling what each absolutely needs to handle. Create more State components if you need them. The Controller will manage all events and invoke the appropriate calls to Action methods.

  • Partition the business logic to keep it separate from the implementation protocol (ideally in separate beans). As this book is focused on the HTTP browser based protocols, consider partitioning the use of resources from javax.servlet.* and javax.servlet.http.* packages into a separate layer from business rules (say, via an adapter design pattern). This allows the business rules to be reused in other architectures such as a GUI based and non- servlet architectures such as RMI or CORBA.

The View

The View represents the presentation logic of an application. The View components obtain the current state of the system from the Model and provide the user interface for the specific protocol involved. For the focus of this book, the protocol we are interested in is HTTP browser based systems.

As part of the generation of the user interface, the View is responsible for presenting the specific set of events that the user may enact at any given moment.

Separating the View from the Model enables the independent construction of user interfaces with different look and feel attributes. These different interfaces can all interact with the same Model. JSPs are a natural choice for implementing the View.

As we have seen, JSPs are a convenient choice for generating HTTP browser based user interfaces. Interaction with the Model (beans) is easy via the built-in beans tags.

The Controller

The Controller provides the glue to the MVC architecture. It is responsible for receiving events, determining the appropriate handler, invoking the handler, and finally triggering the generation of the appropriate response.

With the full power of Java available to us, Servlets are an ideal selection for a Controller technology.

In an MVC architecture, the Controller (servlet) acts as a dispatcher. This presents some challenges that must be addressed. Specifically, the controller must handle the following tasks...

Meet the Author

Matt Raible is a Montana native who grew up in a log cabin without electricity or running water. He would hike to school a mile and a half every day (skiing in the winter), and would arrive home to a very loving family. "The Cabin" is a beautiful place that will always be near and dear to him. Even without electricity, his father connected the family to the Internet using a 300 Baud modem, a Commodore 64, and a small generator. CompuServe was the name, slow was the game. Matt became inspired by the Internet in the early 1990s, and has been developing websites and web applications ever since. He graduated from the University of Denver in 1997 with degrees in Russian, international business, and finance.

Sing Li is a systems consultant, avid open source developer, and active freelance writer. With over two decades of industry experience, Sing is a regular contributor to printed magazines and e-zines, and has a sizable roster of book credits. Sing is an evangelist of the mobile Java, VoIP, and P2P evolution.

Dave Johnson works at HAHT Commerce and is an experienced software developer in the commercial software development, telecommunications, and geographic information systems industries. Dave has been working with Java since before the dawn of Java 1.0. Since then, he has been involved in the development of a number of Java-based commercial products, including the HAHTsite Application Server, HAHT eSyndication, Venetica's Jasper document viewer, and Rogue Wave's Object Factory IDE. Dave is also an active weblogger and the original developer of the open-source Roller Weblogger software. He lives in Raleigh, North Carolina, with his wife and three children.

Daniel Jepp is a senior developer at Dresdner Kleinwort Wasserstein, based in London. He has been working with the Java platform and related technologies for a number of years now, and he has presented several sessions at the JavaOne conference. Dan has recently completed work on Professional SCWCD Certification with coauthor Sam Dalton.

Sam Dalton has worked with Java and related technologies for a number of years, and coauthored Professional Java Servlets 2.3 and Professional SCWCD Certification. He is an active contributor to TagUnit, an open-source custom tag testing framework, and is also pursuing other open source interests. He has just embarked on the next stage of his career adventure by joining ThoughtWorks.

Charles Brown is one of the most noted author, consultant, and trainers in the industry today. His books about Dreamweaver and Fireworks have received critical acclaim and used worldwide as teaching tools. In addition to his work in the IT industry, Charles is also a noted concert pianist, organist, and guitarist appearing in major concert centers world-wide. He began his musical studies at age 4 and went on to study with famed pianist Vladimir Horowitz. At age 14, he made his debut with Leonard Bernstein and later studied at the famed Juilliard School. Eventually he went to Paris to study with the 20th century legend, Igor Stravinsky. While working with Stravinsky, Charles developed a close friendship with one of the most powerful artistic forces of the 20th century: Pablo Picasso. What he learned about creativity from Picasso he uses today in his writings and training work. Charles is a certified Macromedia/Adobe trainer who is in heavy demand worldwide. He frequently speaks at major conferences such as MAX and NAB. You can see his blog at blog.charlesebrown.net and his website can be found at CharlesEBrown.net.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >