JavaServer Pages, 3rd Edition

Book description

JavaServer Pages (JSP) has built a huge following since the release of JSP 1.0 in 1999, providing Enterprise Java developers with a flexible tool for the development of dynamic web sites and web applications. While new point releases over the years, along with the introduction of the JSP Standard Tag Library (JSTL), have incrementally improved the rough areas of the first version of the JSP specification, JSP 2.0 takes this technology to new heights.JavaServer Pages, Third Edition, is completely revised and updated to cover the JSP 2.0 and JSTL 1.1 specifications. It includes detailed coverage of the Expression Language (EL) incorporated into JSP 2.0, the JSTL 1.1 tag libraries and the new function library, the new tag file format that enables custom tag library development without Java code, the simplified Java tag library API, improvements in the JSP XML syntax, and more. Further, it details setup of the Apache Tomcat server, JSP and JSTL syntax and features, error handling and debugging, authentication and personalization, database access, XML processing, and internationalization.This book recognizes the different needs of the two groups of professionals who want to learn JSP: page authors interested in using JSP elements in web pages, and programmers concerned with learning the JSP API and using JSP effectively as a part of an enterprise application. If you're in the first group, you'll learn from the practical web application examples in the second part of the book. If you're in the latter group, you'll appreciate the detailed coverage of advanced topics in the third part, such as how to integrate servlets and JavaBeans components with JSP using the popular Apache Struts MVC framework, and how to develop custom tag libraries using the JSP API, with realistic examples that you can use as a springboard for your own libraries."Hans Bergsten, a JSP expert group veteran and one of our most active contributors, has thoroughly and accurately captured the new features of JSP 2.0 and JSTL 1.1 in a way that is well-organized and easy to understand. With excellent, to-the-point examples, this book is a 'must have' for any serious JSP 2.0 developer."--Mark Roth, JSP 2.0 Specification Lead, Sun Microsystems, Inc.Hans Bergsten is the founder of Gefion Software, a company focused on Java services and products based on J2EE technologies. Hans has been an active participant in the working groups for both the servlet and JSP specifications since their inception and contributes to other related JCP specifications, such as JSP Standard Tag Library (JSTL) and JavaServer Faces (JSF), and, as one of the initial members of the Apache Jakarta Project Management Committee, helped develop the Apache Tomcat reference implementation for the servlet and JSP specifications.

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. What’s in This Book
    2. Readers of the Second Edition
    3. Audience
      1. What You Need to Know
    4. Organization
      1. Part I, JSP Application Basics
      2. Part II, JSP Application Development
      3. Part III, JSP in J2EE and JSP Component Development
      4. Part IV, Appendixes
    5. About the Examples
    6. Conventions Used in This Book
    7. How to Contact Us
    8. Acknowledgments for First Edition
    9. Acknowledgments for Second Edition
    10. Acknowledgments for Third Edition
  3. I. JSP Application Basics
    1. 1. Introducing JavaServer Pages
      1. 1.1. What Is JavaServer Pages?
      2. 1.2. Why Use JSP?
        1. 1.2.1. Embedding Dynamic Elements in HTML Pages
        2. 1.2.2. Compilation
        3. 1.2.3. Using the Right Person for Each Task
        4. 1.2.4. Integration with Enterprise Java APIs
        5. 1.2.5. Other Solutions
          1. 1.2.5.1. Active Server Pages (ASP)
          2. 1.2.5.2. PHP
          3. 1.2.5.3. ColdFusion
          4. 1.2.5.4. Java servlet template engines
        6. 1.2.6. The JSP Advantage
      3. 1.3. What You Need to Get Started
    2. 2. HTTP and Servlet Basics
      1. 2.1. The HTTP Request/Response Model
        1. 2.1.1. Requests in Detail
        2. 2.1.2. Responses in Detail
        3. 2.1.3. Request Parameters
        4. 2.1.4. Request Methods
      2. 2.2. Servlets
        1. 2.2.1. Advantages over Other Server-Side Technologies
        2. 2.2.2. Servlet Containers
        3. 2.2.3. Servlet Contexts and Web Applications
    3. 3. JSP Overview
      1. 3.1. The Problem with Servlets
      2. 3.2. The Anatomy of a JSP Page
      3. 3.3. JSP Processing
        1. 3.3.1. JSP Elements
          1. 3.3.1.1. Directive elements
          2. 3.3.1.2. Standard action elements
          3. 3.3.1.3. Custom action elements and the JSP Standard Tag Library
          4. 3.3.1.4. Scripting elements
          5. 3.3.1.5. Expression Language expressions
          6. 3.3.1.6. JavaBeans components
      4. 3.4. JSP Application Design with MVC
    4. 4. Setting Up the JSP Environment
      1. 4.1. Installing the Java Software Development Kit
      2. 4.2. Installing the Tomcat Server
        1. 4.2.1. Windows Platforms
        2. 4.2.2. Unix Platforms (Including Linux and Mac OS X)
      3. 4.3. Testing Tomcat
      4. 4.4. Installing the Book Examples
      5. 4.5. Example Web Application Overview
  4. II. JSP Application Development
    1. 5. Generating Dynamic Content
      1. 5.1. Creating a JSP Page
      2. 5.2. Installing a JSP Page
      3. 5.3. Running a JSP Page
      4. 5.4. Using JSP Directive Elements
        1. 5.4.1. JSP Comments
      5. 5.5. Using Template Text
      6. 5.6. Using JSP Action Elements
        1. 5.6.1. JSP Standard Tag Library
        2. 5.6.2. The JSP Expression Language
    2. 6. Using JavaBeans Components in JSP Pages
      1. 6.1. What Is a Bean?
      2. 6.2. Declaring a Bean in a JSP Page
      3. 6.3. Reading Bean Properties
        1. 6.3.1. Using the <jsp:getProperty> Action
        2. 6.3.2. Using the JSP Expression Language
        3. 6.3.3. Including Images with JSP
      4. 6.4. Setting Bean Properties
        1. 6.4.1. Using the <jsp:setProperty> Action
        2. 6.4.2. Using the JSTL <c:set> Action
        3. 6.4.3. Automatic Type Conversions
    3. 7. Using Custom Tag Libraries and the JSP Standard Tag Library
      1. 7.1. What Is a Custom Tag Library?
      2. 7.2. Installing a Custom Tag Library
      3. 7.3. Declaring a Custom Tag Library
        1. 7.3.1. Identifying a Custom Tag Library in a JSP 1.1 Container
      4. 7.4. Using Actions from a Tag Library
        1. 7.4.1. Setting Action Attribute Values
        2. 7.4.2. The JSP Standard Tag Library
        3. 7.4.3. Using Beans or Custom Actions
    4. 8. Processing Input and Output
      1. 8.1. Reading Request Parameter Values
        1. 8.1.1. Accessing Parameter Values with JSTL Actions
        2. 8.1.2. Accessing Other Request Data
        3. 8.1.3. Capturing Parameter Values Using a Bean
      2. 8.2. Validating User Input
        1. 8.2.1. Validating User Input Using JSTL Actions
        2. 8.2.2. Validating User Input Using a Bean
      3. 8.3. Formatting HTML Output
    5. 9. Error Handling and Debugging
      1. 9.1. Dealing with Syntax Errors
        1. 9.1.1. Element Syntax Errors
        2. 9.1.2. Expression Language Syntax Errors
      2. 9.2. Debugging a JSP Application
      3. 9.3. Dealing with Runtime Errors
        1. 9.3.1. Catching Exceptions
    6. 10. Sharing Data Between JSP Pages, Requests, and Users
      1. 10.1. Passing Control and Data Between Pages
        1. 10.1.1. Passing Control from One Page to Another
        2. 10.1.2. Passing Data from One Page to Another
        3. 10.1.3. All Together Now
      2. 10.2. Sharing Session and Application Data
        1. 10.2.1. Session Tracking Explained
        2. 10.2.2. Counting Page Hits
        3. 10.2.3. URL Rewriting
      3. 10.3. Online Shopping
        1. 10.3.1. Number Formatting
        2. 10.3.2. Using a Request Parameter as an Index
        3. 10.3.3. Redirect Versus Forward
      4. 10.4. Memory Usage Considerations
    7. 11. Developing Custom Tag Libraries as Tag Files
      1. 11.1. Creating and Using a Tag File
      2. 11.2. Accessing Attribute Values
        1. 11.2.1. Using Undeclared Attributes
      3. 11.3. Processing the Action Body
      4. 11.4. Processing Fragment Attributes
      5. 11.5. Exposing Data to the Calling Page Through Variables
      6. 11.6. Aborting the Page Processing
      7. 11.7. Packaging Tag Files for Easy Reuse
    8. 12. Accessing a Database
      1. 12.1. Accessing a Database from a JSP Page
        1. 12.1.1. Application Architecture Example
        2. 12.1.2. Table Example
        3. 12.1.3. The DataSource Interface and JDBC Drivers
        4. 12.1.4. Reading and Storing Information in a Database
          1. 12.1.4.1. Reading database information
          2. 12.1.4.2. Inserting database information
          3. 12.1.4.3. Updating database information
        5. 12.1.5. Generating HTML from a Query Result
        6. 12.1.6. Searching for Rows Based on Partial Information
        7. 12.1.7. Deleting Database Information
        8. 12.1.8. Displaying Database Data over Multiple Pages
          1. 12.1.8.1. Setting an upper limit for the result size
          2. 12.1.8.2. Getting a limited number of rows at a time
          3. 12.1.8.3. Run a query once and display the result over multiple pages
      2. 12.2. Validating Complex Input Without a Bean
      3. 12.3. Using Transactions
      4. 12.4. Application-Specific Database Actions
    9. 13. Authentication and Personalization
      1. 13.1. Container-Provided Authentication
        1. 13.1.1. Authenticating Users
        2. 13.1.2. Controlling Access to Web Resources
      2. 13.2. Application-Controlled Authentication
        1. 13.2.1. A Table for Personalized Information
        2. 13.2.2. Logging In
          1. 13.2.2.1. Using cookies to remember the username and password
        3. 13.2.3. Authentication Using a Database
          1. 13.2.3.1. Creating the validation object
          2. 13.2.3.2. Setting and deleting cookies
          3. 13.2.3.3. Redirect to the application page
        4. 13.2.4. Checking for a Valid Session
          1. 13.2.4.1. Providing personalized content
        5. 13.2.5. Updating the User Profile
        6. 13.2.6. Logging Out
      3. 13.3. Other Security Concerns
    10. 14. Internationalization
      1. 14.1. How Java Supports Internationalization and Localization
        1. 14.1.1. The Locale Class
        2. 14.1.2. Formatting Numbers and Dates
        3. 14.1.3. Using Localized Text
      2. 14.2. Generating Localized Output
        1. 14.2.1. Using One Page for Multiple Locales
        2. 14.2.2. Using a Separate Page per Locale
      3. 14.3. A Brief History of Bits
      4. 14.4. Handling Localized Input
        1. 14.4.1. Dealing with Non-Western European Input
    11. 15. Working with XML Data
      1. 15.1. Generating an XML Response
      2. 15.2. Transforming XML into HTML
      3. 15.3. Transforming XML into a Device-Dependent Format
      4. 15.4. Processing XML Data
        1. 15.4.1. Caching Data
        2. 15.4.2. Parsing XML Data
        3. 15.4.3. Accessing XML Data Using XPath Expressions
    12. 16. Using Scripting Elements
      1. 16.1. Using page Directive Scripting Attributes
      2. 16.2. Implicit JSP Scripting Objects
      3. 16.3. Using Scriptlets
      4. 16.4. Using Expressions
      5. 16.5. Using Declarations
        1. 16.5.1. jspInit() and jspDestroy( )
      6. 16.6. Mixing Action Elements and Scripting Elements
        1. 16.6.1. Using an Expression Element to Set an Attribute
        2. 16.6.2. Accessing Scoped Variables in Scripting Code
      7. 16.7. Dealing with Scripting Syntax Errors
        1. 16.7.1. Scripting Syntax Error Examples
    13. 17. Bits and Pieces
      1. 17.1. Buffering
      2. 17.2. Including Page Segments
      3. 17.3. Global Configuration Options
        1. 17.3.1. Declaring a File as a JSP Page
        2. 17.3.2. Controlling the Interpretation of EL Expressions
        3. 17.3.3. Controlling the Use of Scripting Elements
        4. 17.3.4. Specifying the File Encoding
        5. 17.3.5. Specifying Automatically Included Files
        6. 17.3.6. Declaring Files as JSP Documents
      4. 17.4. Mixing Client-Side and Server-Side Code
        1. 17.4.1. Generating JavaScript Code
          1. 17.4.1.1. Using server-side data in JavaScript code
        2. 17.4.2. Using Java Applets
      5. 17.5. Precompiling JSP Pages
      6. 17.6. Preventing Caching of JSP Pages
      7. 17.7. Writing JSP Pages as XML Documents
        1. 17.7.1. Identifying a JSP Document
        2. 17.7.2. Custom Tag Library Declarations
        3. 17.7.3. Generating a DOCTYPE Declaration
        4. 17.7.4. XML Syntax for JSP Directives and Scripting Elements
        5. 17.7.5. Generating Elements Dynamically
        6. 17.7.6. Encoding Non-XML Data and Special Characters
        7. 17.7.7. Using the <jsp:root> Element
        8. 17.7.8. XML Declaration Generation
      8. 17.8. How URIs Are Interpreted
  5. III. JSP in J2EE and JSP Component Development
    1. 18. Web Application Models
      1. 18.1. The Java 2 Enterprise Edition Model
      2. 18.2. The MVC Design Model
        1. 18.2.1. Using Only JSP
        2. 18.2.2. Using Servlets and JSP
        3. 18.2.3. Using Servlets, JSP, and EJB
      3. 18.3. Scalability
        1. 18.3.1. Preparing for Distributed Deployment
    2. 19. Combining JSP and Servlets
      1. 19.1. Servlets, Filters, and Listeners
        1. 19.1.1. Servlet Lifecycle
        2. 19.1.2. Compiling and Installing a Servlet
        3. 19.1.3. Reading a Request
        4. 19.1.4. Generating a Response
        5. 19.1.5. Using Filters and Listeners
          1. 19.1.5.1. Filters
          2. 19.1.5.2. Listeners
        6. 19.1.6. Sharing Data Between the Component Types
      2. 19.2. Picking the Right Component Type for Each Task
      3. 19.3. Initializing Shared Resources Using a Listener
      4. 19.4. Access Control Using a Filter
      5. 19.5. Centralized Request Processing Using a Servlet
        1. 19.5.1. Struts Request Processing Overview
        2. 19.5.2. Mapping Application Requests to the Servlet
        3. 19.5.3. Dispatching Requests to an Action Class
        4. 19.5.4. Implementing the Action Classes
        5. 19.5.5. Processing Requests
        6. 19.5.6. Calling the Controller Servlet from JSP Pages
      6. 19.6. Using a Common JSP Error Page
    3. 20. Developing JavaBeans Components for JSP
      1. 20.1. Beans as JSP Components
        1. 20.1.1. JavaBeans Naming Conventions
          1. 20.1.1.1. Handling session events
          2. 20.1.1.2. Using a package name for a bean class
        2. 20.1.2. Compiling and Installing a Bean
      2. 20.2. JSP Bean Examples
        1. 20.2.1. Value Beans
        2. 20.2.2. Utility Beans
        3. 20.2.3. Multithreading Considerations
      3. 20.3. Unexpected <jsp:setProperty> Behavior
    4. 21. Developing Custom Tag Libraries Using Java
      1. 21.1. Developing Simple Tag Handlers
        1. 21.1.1. Accessing Context Information
        2. 21.1.2. Aborting the Page Processing
        3. 21.1.3. Processing the Action Body as an Executable Fragment
          1. 21.1.3.1. Conditional and iterating processing
          2. 21.1.3.2. Processing the action body
        4. 21.1.4. Processing Fragment Attributes
        5. 21.1.5. Handling Exceptions
      2. 21.2. Developing Classic Tag Handlers
        1. 21.2.1. Developing a Basic Action
        2. 21.2.2. Developing an Iterating Action
        3. 21.2.3. Processing the Action Body
          1. 21.2.3.1. Dealing with empty elements
        4. 21.2.4. Handling Exceptions
        5. 21.2.5. The Classic Tag Handler Lifecycle and What It Means to You
          1. 21.2.5.1. Providing default values for optional attributes
          2. 21.2.5.2. Resetting per-invocation state
          3. 21.2.5.3. Keeping expensive resources for the lifetime of the tag handler instance
      3. 21.3. Developing Tag Library Functions
      4. 21.4. Creating the Tag Library Descriptor
        1. 21.4.1. General Library Elements
        2. 21.4.2. Validator and Listener Elements
        3. 21.4.3. Tag Elements
          1. 21.4.3.1. General tag elements
          2. 21.4.3.2. Variable elements
          3. 21.4.3.3. Attribute elements
          4. 21.4.3.4. Dynamic attributes element
          5. 21.4.3.5. Example element
        4. 21.4.4. Tag File Elements
        5. 21.4.5. Function Elements
        6. 21.4.6. Differences Between a JSP 1.2 and a JSP 2.0 TLD
        7. 21.4.7. Differences Between a JSP 1.1 and a JSP 1.2 TLD
      5. 21.5. Packaging and Installing a Tag Library
        1. 21.5.1. Making the Tag Library Files Available to the Container
        2. 21.5.2. Identifying the Tag Library in a JSP Page
        3. 21.5.3. Packaging Multiple Libraries in One JAR File
    5. 22. Advanced Custom Tag Library Features
      1. 22.1. Developing Cooperating Actions
        1. 22.1.1. Using Explicit Parent-Child Cooperation
        2. 22.1.2. Using Implicit Cooperation Through Variables
          1. 22.1.2.1. Creating a scripting variable
          2. 22.1.2.2. Using a TagExtraInfo subclass to declare a variable
        3. 22.1.3. Supporting Undeclared Attributes
      2. 22.2. Validating Syntax
        1. 22.2.1. Validation Based on the TLD
        2. 22.2.2. Using a TagLibraryValidator
        3. 22.2.3. Using a TagExtraInfo Class for Validation
      3. 22.3. Using a Listener in a Tag Library
      4. 22.4. Dynamic Attribute Values and Types
        1. 22.4.1. Conversions Performed by the Container
        2. 22.4.2. Using a PropertyEditor for Conversion
    6. 23. Integrating Custom Code with JSTL
      1. 23.1. Setting and Using Configuration Variables
      2. 23.2. Integrating Custom Conditional Actions
      3. 23.3. Integrating Custom Iteration Actions
        1. 23.3.1. Implementing a Custom Iteration Action
        2. 23.3.2. Interacting with an Iteration Action
      4. 23.4. Integrating Custom I18N Actions
      5. 23.5. Integrating Custom Database Actions
      6. 23.6. Using JSTL Tag Library Validators
    7. 24. Database Access Strategies
      1. 24.1. JDBC Basics
      2. 24.2. Using Connections and Connection Pools
        1. 24.2.1. Using a JDBC 2.0 Optional Package Connection Pool
        2. 24.2.2. Making a JDBC 1.0 Connection Pool Behave as a JDBC 2.0 Connection Pool
      3. 24.3. Making a Connection Pool Available to Application Components
        1. 24.3.1. Using an Application Scope Variable
        2. 24.3.2. Using JNDI
      4. 24.4. Using a Generic Database Bean
      5. 24.5. Developing Application-Specific Database Components
  6. IV. Appendixes
    1. A. JSP Elements Reference
      1. A.1. Directive Elements
      2. A.2. Scripting Elements
      3. A.3. Action Elements
      4. A.4. Custom actions
      5. A.5. Comments
      6. A.6. Escape Characters
    2. B. JSTL Actions and API Reference
      1. B.1. JSTL Library URIs and Default Prefixes
      2. B.2. Core Library Actions
      3. B.3. Internationalization and Formatting Actions
      4. B.4. Database Access Actions
      5. B.5. XML Processing Actions
      6. B.6. EL Functions
      7. B.7. Support and Utility Types
      8. B.8. Configuration Settings
    3. C. JSP Expression Language Reference
      1. C.1. Syntax
        1. C.1.1. Literals
        2. C.1.2. Keywords and Reserved Words
      2. C.2. Variables
        1. C.2.1. Implicit Variables
      3. C.3. Data Types
        1. C.3.1. Coercion Rules
      4. C.4. Expressions and Operators
        1. C.4.1. Operand Coercing Rules
          1. C.4.1.1. Property and array accessor operators
          2. C.4.1.2. Arithmetic operators
          3. C.4.1.3. Relational operators
          4. C.4.1.4. Logical operators
          5. C.4.1.5. Empty operator
    4. D. JSP API Reference
      1. D.1. Implicit Variables
      2. D.2. Other Servlet Types Accessible Through Implicit Variables
      3. D.3. Tag Handler Types
      4. D.4. Translation Time Types
      5. D.5. Other JSP Types
      6. D.6. Expression Language Types
    5. E. Book Example Custom Actions and API Reference
      1. E.1. Generic Custom Actions
      2. E.2. Generic Utility Classes
    6. F. Web Application Structure and Deployment Descriptor Reference
      1. F.1. Web Application File Structure
        1. F.1.1. Placing Java Class Files in the Right Directory
      2. F.2. Web Application Deployment Descriptor
        1. Reference Section
        2. Reference Section
        3. Reference Section
        4. Reference Section
        5. Reference Section
        6. Reference Section
        7. Reference Section
        8. Reference Section
        9. Reference Section
        10. Reference Section
        11. Reference Section
        12. Reference Section
        13. Reference Section
        14. Reference Section
        15. Reference Section
        16. Reference Section
        17. Reference Section
        18. Reference Section
        19. Reference Section
        20. Reference Section
        21. Reference Section
        22. Reference Section
        23. Reference Section
        24. Reference Section
        25. Reference Section
        26. F.2.1. Example Application Deployment Descriptor
      3. F.3. Creating a WAR File
  7. About the Author
  8. Colophon
  9. Copyright

Product information

  • Title: JavaServer Pages, 3rd Edition
  • Author(s): Hans Bergsten
  • Release date: December 2003
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596005634