You are previewing Servlets and JavaServer Pages™: The J2EE™ Technology Web Tier.
O'Reilly logo
Servlets and JavaServer Pages™: The J2EE™ Technology Web Tier

Book Description

Servlets and JavaServer Pages™ is the first complete guide to building dynamic Java-based Web applications using the new JavaServer Pages 2.0 and Servlets 2.4. Servlets and JavaServer Pages (JSP) provide a robust solution to developing large, complex Web applications, including multiserver projects. In addition to built-in security, portability, and a Web server, they offer developers the freedom to work with any operating system that supports Java—be it Linux, Windows, OSX, or Solaris.

This authoritative book begins by explaining how to set up a Servlet and JSP development environment, including a discussion of containers, Java support, and installing and configuring Tomcat. The authors then thoroughly explore servlets and JSP, including significant coverage of custom tag libraries, newly available filters, and popular servlet and JSP design patterns. Readers can then test-drive the knowledge gained by constructing a book-support Web site.

Among the topics covered in-depth are:

  • Exception handling

  • JavaBeans and the new JSP Expression Language (JSP EL)

  • The JavaServer Pages Standard Tag Library (JSTL) and coding custom tag libraries

  • Filters

  • Error handling

  • Session and state management

  • Security

  • Design patterns, including Struts

  • Internationalization

  • Multiclient support

  • Database connectivity

The actual book-support site (www.jspbook.com) features frequently asked questions, working code examples, and references.

With Servlets and JavaServer Pages™ as their guide, readers will learn how to fully harness the power of these flexible Java technologies.



0321136497B05082003

Table of Contents

  1. Copyright
    1. Dedication
  2. Preface
    1. About the Authors
      1. Jayson Falkner
      2. Kevin Jones
    2. How the Book Is Organized
      1. The Book Support Web Site
      2. Conventions Used in the Book
        1. InTextCode Style
        2. Foo
        3. Mozilla and Tomcat
    3. The Book's Cover
      1. Yard-Gnomes on Holiday and Yard-Gnomes in France
    4. Special Thanks
  3. 1. Setting Up a Servlet and JSP Environment
    1. A Quick History of Web Development
      1. CGI
      2. Java Servlets
    2. Containers
    3. Getting Java Support
      1. Downloading the Java 2 Standard Edition 1.4
      2. Installing J2SE 1.4 on Microsoft Windows
      3. Installing J2SE 1.4 on Linux Distributions
        1. For Non-x86 Users: Compiling Java for Yourself
      4. Tomcat
        1. Installing Jakarta Tomcat 5 on Windows
        2. Windows 9X/ME “out of environment space”
        3. Installing Jakarta Tomcat 5 on Linux and Macintosh OS X
      5. Configuring Tomcat
        1. Switching Tomcat to Port 80, the Default HTTP Port
        2. Tomcat User's Guide
        3. Tomcat User Mailing List
    4. Web Applications
      1. /WEB-INF and web.xml
      2. Java Classes and Source Files
      3. Java Archive (JAR) Files
      4. Web Application Resource (WAR) Files
    5. Ant
      1. What Does Ant Do?
      2. Installing Ant
      3. Using Ant
    6. Summary
  4. 2. Java Servlets
    1. What Servlets Are and Why You Would Want to Use Them
      1. Web Applications
      2. Servlets and HTTP Servlets
      3. Filters
      4. Security
      5. Internationalization
    2. Servlet Life Cycle
    3. Servlets for the World Wide Web
      1. Requests, Responses, and Headers
      2. GET and POST
      3. HTTP Response Codes
    4. Coding an HttpServlet
      1. Deploying a Servlet
        1. Understand Servlet Deployment!
      2. Web Application Deployment Descriptor Structure
    5. Servlet Configuration
      1. Limitations of Configuration: web.xml Additions
      2. Client/Server Servlet Programming
      3. HttpServletRequest and HttpServletResponse
      4. HttpServletResponse
        1. Response Headers
        2. Response Redirection
        3. Response Redirection Translation Issues
        4. Auto-Refresh/Wait Pages
      5. HttpServletRequest
        1. Headers
        2. Form Data and Parameters
        3. File Uploads
        4. Using a File Upload API
        5. Request Delegation and Request Scope
    6. ServletContext
      1. Initial Web Application Parameters
        1. Application Scope
        2. Virtual Directory Translation
        3. Application-Wide Logging
        4. Distributed Environments
        5. Temporary Directory
    7. Servlet Event Listeners
    8. Summary
  5. 3. JavaServer Pages
    1. JSP 2.0 Specification
      1. JSP
      2. JavaBeans
      3. Custom Tags and JSP Fragments
      4. Expression Language
    2. JSP Life Cycle
    3. The Difference Between Servlets and JSP
    4. JSP Syntax and Semantics
      1. Elements and Template Data
      2. Two Types of Syntax
      3. Scripting Elements
        1. Scriptlets
        2. Expressions
        3. Declarations
        4. Good Coding Practice with Scripting Elements
      4. Directives
        1. <%@ page %>
          1. language
          2. extends
          3. import
          4. session
          5. buffer
          6. autoFlush
          7. isThreadSafe
          8. isErrorPage
          9. errorPage
          10. contentType
          11. pageEncoding
          12. isScriptingEnabled
          13. isELEnabled
        2. <%@ include %> and <jsp:include />
        3. <%@ taglib %>
      5. JSP Configuration
        1. el-enabled
        2. scripting-enabled
        3. page-encoding
        4. include-prelude
        5. include-coda
        6. is-xml
        7. Application-Wide Headers and Footers
      6. Standard JSP Actions
        1. <jsp:include/>
        2. <jsp:plugin/>, <jsp:fallback/>, <jsp:params/>, and <jsp:param/>
          1. type
          2. code
          3. codebase
          4. align
          5. archive
          6. height
          7. hspace
          8. jreversion
          9. name
          10. vspace
          11. width
        3. <jsp:forward/>
        4. <jsp:forward/> and <jsp:include/> parameters
        5. JavaBean Actions
        6. Tag File Actions
      7. Whitespace Preservation
      8. Attributes
      9. Comments
      10. Quoting and Escape Characters
    5. Implicit Objects
      1. config
      2. request
      3. response
      4. session
      5. application
      6. pageContext
        1. Request Delegation
        2. Page Scope
      7. out
        1. Buffering
          1. Flush the Buffer
          2. Throw an Exception
        2. JspWriter and Response Committed Exceptions
          1. Manipulating Headers
          2. Forwarding
      8. config
        1. Initial Configuration Parameters
      9. page
    6. JSP in XML Syntax
      1. XML Rules
      2. JSP Documents
        1. JSP Document Declaration
        2. Scripting Elements
        3. Directives
        4. Encapsulating Template Text
    7. Summary
  6. 4. Exception Handling
    1. Errors and Exceptions
      1. Throwing Exceptions
        1. Subclassing Exception
        2. Consolidation of Throwable Classes
      2. Try, Catch, Finally
        1. Finally
    2. JSP and Servlet Exceptions
    3. Web Application Exception Handling
      1. Micro-Managing Exceptions
        1. Error Handling JSP
        2. Error Handling Servlets
      2. Macro-Managing Exceptions
      3. Web Application Error Pages
        1. Exception-Based Error Pages
        2. HTTP Status Code–Based Error Pages
        3. Customizing Web Application Exception Pages
        4. Exception Handling Priorities
    4. Logging
      1. The Problem with System.out.println()
      2. JDK 1.4 Logging Versus Log4j
      3. Using the java.util.logging Package
      4. Handlers
        1. Formatting
      5. Loggers
        1. Levels of Logged Information
        2. Custom Web Application Logging
    5. Logging and Performance
      1. A General Philosophy for Exception Handling and Logging
    6. Summary
  7. 5. JavaBeans and the JSP Expression Language
    1. JavaBeans
    2. Get and Set Methods
      1. Why Get and Set Methods?
    3. Servlets, JSP, and JavaBeans
      1. <jsp:useBean/>
        1. Using <jsp:useBean/>
      2. <jsp:getProperty/> and <jsp:setProperty/>
        1. <jsp:getProperty/>
        2. <jsp:setProperty/>
        3. Bean Initialization
    4. Good Use of JavaBeans
      1. Design Patterns and JavaBeans
    5. JSP. 2.0 Expression Language
      1. Disabling the EL
      2. JSP EL Syntax
        1. Attributes
        2. Literals
        3. Operators
      3. Reserved Words
      4. EL Functions
      5. Good Uses of the JSP EL
        1. The JSP EL Doesn't Require JavaBeans
    6. Summary
  8. 6. JavaServer Pages Standard Tag Library
    1. JSTL 1.0 Specification
    2. Why You Should Use the JSTL
    3. Installing the JSTL
    4. JSTL Expression Language
      1. Twin Libraries
    5. Core Tags
      1. General-Purpose Tags
        1. <c:out>
        2. <c:set>
        3. <c:remove>
        4. <c:catch>
      2. Iteration
        1. <forEach>
        2. <forTokens>
      3. Conditionals
        1. <c:if>
        2. <choose>, <when>, and <otherwise>
      4. URL Manipulation
        1. <c:import> and <c:param>
        2. <c:param>
        3. <c:url>
        4. <redirect>
    6. i18n-Capable Text Formatting
    7. XML Manipulation
    8. SQL Tags
    9. Justification for Skipping the SQL Tags
    10. Summary
  9. 7. Custom Tag Libraries
    1. Why Custom Tags?
    2. Tag Library Basics
    3. How Are Tags Being Used?
      1. MVC
      2. Internationalization
      3. Multi-Client Support
      4. Conditionals, Iterations, and Simple, Needed Tasks
      5. Countless Illogical Ways
    4. New and Old Custom Tags
    5. Tag Library Descriptors (TLDs)
      1. What Is a Tag Library Descriptor?
      2. Using a Tag Library Descriptor
        1. Relative URI
        2. Web Application-Defined URI
        3. XML Namespaces and JSP in XML Syntax
    6. Simple JSP 2.0 Custom Tags
      1. SimpleTag Interface
      2. Attributes
        1. Static Attributes and TLD Declaration
        2. Dynamic Attributes
        3. Runtime Attribute Values
      3. Body Evaluation and Iteration
      4. .tag Files
        1. What Is a .tag File?
        2. Coding a .tag File
        3. <%@ tag %>
        4. <%@ attribute %>
        5. <%@ variable %>
        6. <jsp:doBody/>
      5. Cooperating Tags
    7. Classic JSP Tag Handlers
      1. Basic Tags
        1. Tag Life Cycle
        2. Tag Interface
          1. int doStartTag()
          2. int doEndTag()
          3. void release()
          4. Tag getParent()
          5. void setParent(Tag t)
          6. void setPageContext(PageContext p)
      2. Coding a BasicTag
        1. Tag Handler Exception Handling
        2. TagSupport
        3. Using a Tag Handler and TLD with JSP
      3. Re-Using Tags
      4. TryCatchFinally Interface
      5. Cooperating Tags
      6. Mixing New Tags with Classic Tags
        1. </mt:simpleTag>
        2. </mt:classicTag>
      7. Iteration Tags
        1. IterationTag Interface
        2. Coding an Iteration Tag
      8. Body Tags
        1. BodyTag Interface
        2. Coding a Body Tag
        3. Nested Body Tag Ambiguities
      9. Tag Scripting Variables
        1. TLD-Declared Scripting Variables
        2. TagExtraInfo Classes
      10. Tag Library Listeners
      11. Validation
    8. Summary
  10. 8. Filters
    1. Introducing Filters
      1. What Is a Filter?
      2. The Filter Life Cycle
      3. Coding a Simple Filter
        1. Filter Chains
        2. Filter Configuration
        3. Filters and Request Dispatching
    2. Wrappers
      1. Request Wrapping
      2. Response Wrapping
        1. Compression Filters
        2. Compression: What Has Been Gained?
        3. Cache Filters
        4. Caching Servlet Responses
        5. Caching and Fault Tolerance
    3. Filters That Provide JSP-Replacing Functionality
    4. Summary
  11. 9. Managing State in a Web Application
    1. HTTP and Session State
      1. Using an ID to Identify Clients
      2. javax.servlet.http.HttpSession
        1. Session Scope
      3. Cookies
        1. Tracking Users via Cookies
        2. URL Rewriting
      4. Initializing Session Resources
        1. Persisting and Loading Sessions
        2. Knowing If an Object Is Bound to Session
        3. Session Time-outs
    2. Persistent State
      1. Session Smearing
      2. Sharing State Information via a Database
    3. State and Thread Safety
      1. Synchronizing
      2. Protecting Servlet State
      3. javax.servlet.SingleThreadModel
      4. Protecting Session and Application State
    4. Summary
  12. 10. Security
    1. What Do We Mean by Security?
    2. Declarative Security
      1. Role-Based Security
      2. Configuring Realms
      3. The Big Picture
      4. Configuring Basic or Digest Authentication
      5. Custom Form-Based Authentication
    3. Programmatic Security in a Servlet/JSP
    4. Secure Encrypted Communication
      1. Specifying HTTPS
    5. How Secure Is Security?
    6. Encryption and Compression and Caching
    7. Summary
  13. 11. Design Patterns
    1. Why Use a Design Pattern?
    2. Common Design Patterns
      1. Model 1
        1. Simple Model 1 Web Site
        2. Why Is This Model 1?
        3. Model 1 Weaknesses
      2. Model 2
        1. Simple Model 2 Web Site
        2. Why Is This Model 2?
        3. Model 2 Strengths
        4. Model 2 Weaknesses
        5. Caveats of This Example
      3. Good Model 2 Implementation
    3. Jakarta Struts
      1. Installing Struts
        1. What Is in Struts 1.0.2?
        2. Creating a New Struts Web Application
        3. Installing Struts with an Existing Web Application
      2. Struts Control Servlet
      3. Actions
        1. Using Action Classes
      4. Using Struts
      5. Model 1½
        1. Weaknesses of Model 1½
    4. Abstracting DHTML via Custom Tags
      1. Why Abstract DHTML with Custom Tags?
      2. Coding DHTML Widget Custom Actions
    5. Summary
  14. 12. Internationalization
    1. Content Encoding
      1. ISO-8859-1
      2. Unicode
        1. UTF-8 and UCS-2
      3. Working with Non-ISO-8859-1 Encoding
        1. Unicode Escapes
    2. i18n Implementation Techniques
      1. Language Detection
        1. Inquiring
        2. HTTP Accept-Language and Accept-Charset Headers
        3. Persisting Language Information and URL Encoding
      2. Multiple Pages
      3. Content Abstraction
        1. Resource Bundles
        2. JSTL i18n Message Tags
    3. Number and Date Formatting
      1. i18n Numbers and Dates at Runtime
      2. Using DateFormat, NumberFormat, and MessageFormat
        1. Formatting Messages Using MessageFormat
      3. JSTL Number and Date Formatting Tags
        1. JSTL Message Tag with Parameters
    4. Summary
  15. 13. Multi-Client Support
    1. Who Should Read This Chapter
    2. Separating Format from Content
    3. Implementing Multi-Client Support
      1. Templates
        1. Benefits and Drawbacks of Templates
      2. Transformations
        1. Implementing Transformations
        2. Installing the Multi-Client Tags
    4. Solving Multi-Client Problems
      1. Creating a Multi-Client Interface
        1. Transformations
        2. Detecting Client Types
        3. Printer-Friendly Formats
        4. URL Rewriting
        5. URL Rewriting Using the MCT Framework
      2. Non-Text Formats
    5. Summary
  16. 14. Database Connectivity
    1. What Is a Database?
      1. SQL
        1. Installing hsqldb
        2. Making a Database
      2. CRUD
        1. INSERT
        2. SELECT
        3. UPDATE
        4. DELETE
        5. The SQL That Was Skipped
    2. JDBC
      1. javax.sql.DataSource
        1. Configuring a DataSource for Use on Tomcat
        2. Upgrading CreateDatabase.jsp and ArbitrarySQL.jsp
        3. DataSources and JNDI
      2. java.sql.Connection and java.sql.Statement
        1. Manage Connections Carefully
        2. SQL Transactions
        3. Database Meta-Data
      3. java.sql.ResultSet
        1. ResultSet Meta-Data
        2. Multiple Results
      4. A Simple JDBC-Based Application
        1. Making Use of the URI and LINK Tables
        2. Why Put Links in a Database?
    3. Using JDBC Optimally
      1. Connection Pooling
      2. Optimized Statements
        1. Prepared Statements
        2. Stored Procedures
      3. Database Administrators Are Expected to Know a Lot
    4. JDBC Web Application Design Patterns
      1. Data Access Objects Design Pattern
        1. Implementing the DAO Design Pattern
        2. DAO Factories
    5. Summary
  17. 15. Building a Complete Web Application
    1. Designing a Web Application
      1. Physical Implementation
        1. Create a Web Application for the Book Support Site
    2. Distributing the Workload: Dividing Up Who Does What
      1. Practical Use of Web Application Labor Division
    3. Implementing Database Support: Creating a Database and Using JDBC
      1. Database Physical Design
        1. Database Creation SQL
      2. Interfacing without SQL
    4. Implementing Business Logic: Filters and the Model 2 Design Pattern
      1. Coding Model 2 Logic Classes and Populating Request-Scoped Variables
        1. An Abstract Logic Component
        2. Logic Component for index.jsp
        3. Logic Components for Adding and Editing News
        4. Logic Component for faq.jsp
        5. Logic Components for Adding and Editing FAQ
        6. Logic Components for the Feedback and Errata Pages
      2. Dealing with Overly Complex Logic Components
    5. Implementing Presentation Logic: JSP, Multi-Client Design, and Internationalization
      1. Building a Simple Presentation Page
        1. Creating MCT Headers and Footers
        2. Adding Some More Style, Cascading Style Sheets
        3. Letting a User Suggest News
      2. Creating the Other Presentation Pages
        1. Coding the About the Book Page
        2. Coding the Other Static Pages: Free Chapters Page and Code Page
        3. Errata Page
        4. Letting Users Suggest Errata
        5. Coding the Other Dynamic Pages: FAQ Pages and Feedback Pages
      3. Localized Content
        1. En Francais
    6. (in Japanese)
    7. Finishing the Site
      1. Site-Wide Error Handling
        1. Logging Errors
      2. Adding Security
      3. Link Tracking
      4. Caching and Compression
        1. Don't Cache Always-Dynamic Resources!
      5. Adding the Egg
    8. Summary