Unformatted text preview: JavaServer Pages Hans Bergsten First Edition, December 2000 ISBN: 1-56592-746-X, 572 pages
JavaServer Pages shows how to develop Java-based web applications without having to be a
hardcore programmer. The author provides an overview of JSP concepts and illuminates how JSP
fits into the larger picture of web applications.
There are chapters for web authors on generating dynamic content, handling session information,
and accessing databases, as well as material for Java programmers on creating Java components
and custom JSP tags for web authors to use in JSP pages.JavaServer Pages shows how to develop
Java-based web applications without having to be a hardcore programmer.
The author provides an overview of JSP concepts and illuminates how JSP fits into the larger
picture of web applications. There are chapters for web authors on generating dynamic content,
handling session information, and accessing databases, as well as material for Java programmers
on creating Java components and custom JSP tags for web authors to use in JSP pages. Release Team[oR] 2001 Preface What's in This Book
About the Examples
Conventions Used in This Book
How to Contact Us
Acknowledgments 1 i JSP Application Basics 1 Introducing JavaServer Pages 2 HTTP and Servlet Basics 13 JSP Overview 25 4 Setting Up the JSP Environment 34 ii JSP Application Development 5 Generating Dynamic Content 42 6 Using Scripting Elements 55 Error Handling and Debugging 74 Sharing Data Between JSP Pages, Requests, and Users 87 3 7 8 This part of the book describes the fundamentals of HTTP (the protocol used by all web applications),
how servlets and JSP are related, and how to set up a JSP development environment and install the
book examples. 1.1
4.5 What Is JavaServer Pages?
Why Use JSP?
What You Need to Get Started The HTTP Request/Response Model
Packaging Java Web Applications The Problem with Servlets
The Anatomy of a JSP Page
JSP Application Design with MVC Installing the Java Software Development Kit
Installing the Tomcat Server
Installing the Book Examples
Example Web Application Overview 8 The focus of this part of the book is on developing JSP-based web applications using both standard JSP
elements and custom components. Through the use of practical examples, you will learn how to handle common
tasks such as validating user input, accessing databases, authenticating users and protecting web pages,
localizing your web site, and more. 5.1
8.5 What Time Is It?
Input and Output Java Primer
Implicit JSP Objects
Using an Expression to Set an Attribute
Declaring Variables and Methods Dealing with Syntax Errors
Debugging a JSP-Based Application
Dealing with Runtime Errors Passing Control and Data Between Pages
Sharing Session and Application Data
Using Custom Actions
Memory Usage Considerations 9 Database Access
9.4 Accessing a Database from a JSP Page
Input Validation Without a Bean
Application-Specific Database Actions 109 10 Authentication and Personalization 130 11 Internationalization 148 12 Bits and Pieces 165 10.1
12.7 Container-Provided Authentication
Other Security Concerns How Java Supports Internationalization and Localization
Generating Localized Output
A Brief History of Bits
Handling Localized Input Buffering
Including Page Fragments
XML and JSP
Mixing Client-Side and Server-Side Code
Precompiling JSP Pages
Preventing Caching of JSP Pages
How URLs Are Interpreted III: JSP in J2EE and JSP Component Development If you're a programmer, this is the part of the book where the real action is . Here you will learn how to develop
your own custom actions and JavaBeans, and how to combine JSP with other Java server-side technologies,
such as servlets and Enterprise JavaBeans (EJB). 13 Web Application Models 182 14 Combining Servlets and JSP 190 15 Developing JavaBeans for JSP 200 16 Developing JSP Custom Actions 213 17 Developing Database Access Components 235 13.1
17.4 The Java 2 Enterprise Edition Model
The MVC Model
Scalability Using a Servlet as the Controller
A More Modular Design Using Action Objects
Sharing Data Between Servlets and JSP Pages
Using a JSP Error Page for All Runtime Errors JavaBeans as JSP Components
JSP Bean Examples
Unexpected <jsp:setProperty> Behavior Tag Extension Basics
Developing a Simple Action
Processing the Action Body
Letting Actions Cooperate
Creating New Variables Through Actions
Developing an Iterating Action
Creating the Tag Library Descriptor
How Tag Handlers May Be Reused
Packaging and Installing a Tag Library Using Connections and Connection Pools
Using a Generic Database Bean
Developing Generic Database Custom Actions
Developing Application-Specific Database Components iv Appendixes A JSP Elements Syntax Reference 260 B JSP API Reference 270 C Book Example Custom Actions and Classes Reference 312 D Web-Application Structure and Deployment Descriptor Reference 337 E JSP Resource Reference 346 Colophon 350 In this part of the book, you find reference material, such as descriptions of JSP elements and classes, all book
example components, the web application deployment descriptor, and more. A.1
E.3 Directive Elements
Escape Characters Implicit Variables
Servlet Classes Accessible Through Implicit Variables
Tag Extension Classes
Other JSP Classes Generic Custom Actions
Internationalization Custom Actions
Database Custom Actions
Database Access Classes Web Application File Structure
Web Application Deployment Descriptor
Creating a WAR File JSP-Related Products
Information and Specifications JavaServer Pages (JSP) technology provides an easy way to create dynamic web pages. JSP uses a componentbased approach that allows web developers to easily combine static HTML for look-and-feel with Java components
for dynamic features. The simplicity of this component-based model, combined with the cross-platform power of
Java, allows a web development environment with enormous potential.
JavaServer Pages shows how to develop Java-based web applications without having to be a hardcore
programmer. The author provides an overview of JSP concepts and discusses how JSP fits into the larger picture
of web applications. Web page authors will benefit from the chapters on generating dynamic content, handling
session information, accessing databases, authenticating users, and personalizing content. In the programmingoriented chapters, Java programmers learn how to create Java components and custom JSP tags for web authors
to use in JSP pages. JavaSercer Pages
JavaServer Pages™ (JSP) is a new technology for web application development that has received a great deal
of attention since it was first announced.
Why is JSP so exciting? One reason is that JSP is Java-based, and Java is well-suited for enterprise computing.
In fact, JSP is a key part of the Java™ 2 Enterprise Edition (J2EE) platform and can take advantage of the
many Java Enterprise libraries, such as JDBC, JNDI, and Enterprise JavaBeans™.
Another reason is that JSP supports a powerful model for developing web applications that separates
presentation from processing. Understanding why this is so important requires a bit of a history lesson. In the
early days of the Web, the only tool for developing dynamic web content was the Common Gateway Interface
(CGI). CGI outlined how a web server made user input available to a program, as well as how the program
provided the web server with dynamically generated content to send back. CGI scripts were typically written in
Perl. (In fact, CGI Perl scripts still drive numerous dynamic web sites.) However, CGI is not an efficient
solution. For every request, the web server has to create a new operating-system process, load a Perl
interpreter and the Perl script, execute the script, and then dispose of the entire process when it's done.
To provide a more efficient solution, various alternatives to CGI have been added to programmers' toolboxes
over the last few years: FastCGI, for example, runs each CGI program in an external permanent process (or a
pool of processes). In addition, mod_perl for Apache, NSAPI for Netscape, and ISAPI for Microsoft's IIS all run
server-side programs in the same process as the web server itself. While these solutions offer better
performance and scalability, each one is supported by only a subset of the popular web servers.
The Java Servlet API, introduced in early 1997, provides a solution to the portability issue. However, all these
technologies suffer from a common problem: HTML code embedded inside programs. If you've ever looked at
the code for a servlet, you've probably seen endless calls to out.println( ) that contain scores of HTML tags.
For the individual developer working on a simple web site this approach may work fine, but it makes it very
difficult for people with different skills to work together to develop a web application.
This is becoming a significant problem. As web sites become increasingly complex and are more and more
critical to the success of an organization, the appearance and usability of the web interface becomes
paramount. New client technologies, such as client-side scripts and DHTML, can develop more responsive and
interactive user interfaces, stylesheets can make it easier to globally change fonts and colors, and images can
make the interface more appealing. At the same time, server-side code is getting more complex, and demands
for reliability, performance, and fault tolerance are increasing. The growing complexity of web applications
requires a development model that allows people with different skills to cooperate efficiently.
JavaServer Pages provides just such a development model, allowing web page authors with skills in graphics,
layout, and usability to work in tandem with programmers who are experienced in server-side technologies
such as multithreading, resource pooling, databases, and caching. While there are other technologies, such as
ASP, PHP, and ColdFusion, that support similar development models, none of them offers all the advantages of
JSP. What's in This Book
This book covers Version 1.1 of the JavaServer Pages specification, which was released in late 1999.
In this book, you will learn how to use all the standard JSP elements and features, including elements for
accessing JavaBeans components, separating the processing over multiple pages to increase reusability and
simplify maintenance, and sharing information between pages, requests, and users. You will also learn how to
use and develop custom components. A rich set of custom components, for tasks such as integration of
database data, internationalization, access control, and conditional processing, is described in detail. Many of
these components are generic enough that you can reuse them directly in your own applications.
The examples in this book guide you through solutions to common JSP design problems, from basic issues such
as retrieving and validating user input, to more advanced areas such as developing a database-driven site,
authenticating users, providing personalized content, and implementing internationalization. The last part of
the book describes how you can combine JSP with other Java technologies; in particular, I describe the
combination of JSP and servlets and provide an overview of how JSP fits into the larger scope of J2EE. page 1 JavaSercer Pages
This book is for anyone interested in using JSP technology to develop web applications. In particular, it is
written to help the two types of people commonly involved in the development of a JSP-based application:
Page authors primarily develop the web interface to an application. This group uses HTML, stylesheets,
and client-side code to develop a rich user interface, and wants to learn how to use JSP elements in
web pages to interact with the server components of the application, such as databases and Enterprise
Java programmers are comfortable with the Java programming language and Java servlets. This group
is interested in learning how to develop JSP components that page authors can use in web pages, such
as JSP custom actions and JavaBeans, and how to combine JSP with other Java server-side
technologies, such as servlets and EJB.
This book is structured into three parts, which I describe shortly, to make it easier to find the material you are
most interested in. What You Need to Know
It's always hard to assume how much you, as the reader, already know. For this book, it was even harder,
since the material is intended for two audiences: page authors and programmers.
I have assumed that anyone reading this book has experience with HTML; consequently, I will not explain the
HTML elements used in the examples. But even if you're an HTML wiz, this may be your first exposure to
dynamic web content and web applications. A thorough introduction to the HTTP protocol that drives all web
applications, as well as to the concepts and features specific to servlet and JSP-based web applications, is
therefore included. If you want to learn more about HTML, I recommend HTML and XHTML: The Definitive
Guide, by Chuck Musciano and Bill Kennedy (O'Reilly & Associates).
If you're a page author, I have assumed that you don't know anything about programming, although it doesn't
This book contains a brief Java primer with enough information to allow you to use a modest amount of Java
code in JSP pages. As you will see, I recommend that you use Java components developed by a Java
programmer instead of putting your own Java code in the pages, so you don't have to know all the intricate
details of the Java language to use JSP.
I have assumed that programmers reading this book are familiar with Java programming, object-oriented
concepts, and Java servlets. If you plan to develop JSP components for page authors and are not familiar with
Java programming, I recommend that you read an introductory Java book, such as Exploring Java by Patrick
Niemeyer and Joshua Peck (O'Reilly). If you need to learn about servlets, I recommend Java Servlet
Programming by Jason Hunter and William Crawford (O'Reilly) or another book that covers servlet technology.
The chapters dealing with database access require some knowledge of SQL and databases in general. I will
explain all that you need to know to run the examples, but if you're hoping to develop database-driven
applications, you will need to know more about databases than what's in this book. page 2 JavaSercer Pages
This book is structured into three parts. The first part describes the fundamentals of HTTP (the protocol used
by all web applications), how servlets and JSP are related, and how to set up a JSP development environment.
The focus of the second part is on developing JSP-based web applications using both standard JSP elements
and custom components. Through practical examples, you will learn how to handle common tasks such as
validating user input, accessing databases, authenticating users and protecting web pages, localizing your web
site, and more. This portion of the book is geared more towards web content designers.
In the third part, you will learn how to develop your own custom actions and JavaBeans, and how to combine
JSP with other Java server-side technologies, such as servlets and Enterprise JavaBeans (EJB). This portion of
the book is targeted towards the programming community.
All in all, the book consists of 17 chapters and five appendixes as follows. Part I, JSP Application Basics
Explains how JSP fits into the big picture of web applications and how it compares to alternative
Describes the fundamental HTTP and servlet concepts you need to know to use JSP to its full potential.
An overview of the JSP features, as well as the similarities and differences between JSP pages and
servlets. Also introduces the Model-View-Controller design model and how it applies to JSP.
Describes where to get the JSP reference implementation, Apache Tomcat, and how to set it up on your
system. Also explains how to install the book examples. Part II, JSP Application Development
Explains how to use JSP to generate dynamic content and how to receive and validate user input.
A brief introduction to Java programming, followed by descriptions of all the JSP elements that let you
embed Java code directly in your JSP pages.
Describes the kinds of errors you may encounter during development of a JSP-based application, and
strategies and JSP features that help you deal with them.
Explains the JSP features that let you separate different types of processing in different pages to
simplify maintenance and further development. Also describes how sessions can be used to build up
information over a sequence of requests from the same user, and how information that applies to all
users can be shared using the application scope. page 3 JavaSercer Pages
A quick overview of relational databases, JDBC, and SQL basics. Introduces a set of generic custom
actions for reading, updating, and deleting database data.
Describes how authentication and access control can be implemented using container-provided and
application-controlled mechanisms, and how to use information about the current user to personalize
the web pages.
Explains internationalization and localization, as well as the Java features available to implement an
internationalized application. Describes a set of custom actions used to implement a web site with
support for multiple languages.
Covers various areas not discussed in previous chapters, such as using XML and XSL with JSP,
combining JSP with client-side code, reusing JSP fragments by including them in JSP pages,
precompiling JSP pages, and more. Part III, JSP in J2EE and JSP Component Development
An overview of J2EE and web application architectures using JSP in combination with other Java
Describes in detail how JSP can be combined with servlets.
Provides details about JavaBeans as they relate to JSP, including threading and synchronization
concerns for session and application-scope JavaBeans, as well as how using JavaBeans can make it
easier to eventually migrate to an EJB architecture. The beans used in previous chapters are reused as
Describes the JSP Tag Extension mechanism and how it is used to develop custom actions, reusing
many of the custom actions from previous chapters as examples.
Describes the database-access custom actions used in the previous chapters and how to use them with
both connection pools developed in-house and those provided by a third-party vendor. Also explains
how you can reuse the database-access beans to develop your own application-specific database
custom actions. page 4 JavaSercer Pages
Part IV, Appendixes
Contains descriptions of all the standard JSP 1.1 elements.
Contains descriptions of all implicit objects available in a JSP page as defined by the servlet and JSP
APIs, as well as the tag extension mechanism classes and interfaces.
Contains descriptions of the custom actions, beans, and utility classes used in the examples.
Contains descriptions of the standar...
View Full Document