You are previewing Sams Teach Yourself JavaServer Pages™ in 21 Days.
O'Reilly logo
Sams Teach Yourself JavaServer Pages™ in 21 Days

Book Description

Sams Teach Yourself JavaServer Pages in 21 Days offers a proven tutorial format to teach JSP in 21 example-driven lessons. This book is for Web publishing professionals migrating to JavaServer Pages for its ability to create dynamic, interactive Web sites and separate presentation from Java code running behind the scenes. The book starts by explaining the relationship between JSP and Java Servlets and the basics of JSP functions and features. You'll then learn how JSP handles data, interacts with Java components, tracks users, and more. Later chapters discuss debugging, working with databases, XSLT and XML, using the Struts framework from Apache, handling binary data like graphics, and deploying JSP applications. Each topic is illustrated with many working examples that the reader can understand and put to work immediately. Throughout the book the author provides pointers to upcoming developments in JSP 2, to ensure that you are prepared for changes in the new version.

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Author
  3. Acknowledgments
  4. We Want to Hear from You!
  5. Introduction
    1. Who Is This Book For?
    2. What's Inside?
    3. What Do You Need?
    4. Where to Download the Code for This Book
    5. Conventions Used in This Book
    6. A Note From the Author
  6. 1. At a Glance
    1. 1. Getting Started!
      1. What Are JavaServer Pages Good For?
      2. It All Starts with Java
      3. The Tomcat Server
        1. The Tomcat Directory Structure
        2. Starting Tomcat
      4. Creating Web Documents
      5. A Brief History of JSP
        1. HTML
        2. JavaScript
        3. Java
        4. Java Applets
        5. Java Servlets
        6. JSP
      6. Dissecting Our First JSP
      7. Overview of JSP Syntax
        1. Scripting Elements
          1. Scriptlets
          2. Declarations
          3. Expressions
        2. Comments
        3. Directives
        4. Actions
      8. Online JSP Resources
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Exercises
    2. 2. Handling Data and Operators
      1. Java in JSP
      2. The JSP Programming Environment
      3. Handling Data in JSP
      4. Creating Variables
      5. Initializing Variables
      6. Data Types
      7. Converting Between Data Types
        1. Automatic Conversions
        2. Performing Type Conversions
      8. Strings
      9. Creating Strings
      10. Strings Are Objects
      11. Determining String Length
      12. Creating and Working with Arrays
      13. Working with Operators
      14. Assignment Operators
      15. Incrementing and Decrementing Operators
      16. Multiplication and Division Operators
      17. Addition and Subtraction Operators
      18. Relational Operators
      19. Logical Operators
      20. Understanding Operator Precedence
      21. Summary
      22. Q&A
      23. Workshop
        1. Quiz
        2. Exercises
    3. 3. Branching, Looping, and Creating Methods
      1. Branching Statements
        1. The if Statement
          1. Executing Compound Statements
          2. The else Statement
          3. Nested if Statements
          4. if-else Ladders
        2. The switch Statement
      2. Loops
        1. The for Loop
        2. The while Loop
        3. The do-while Loop
        4. Using the break Statement to End a Loop
        5. Using the continue Statement to Skip Iterations
      3. Creating Methods
        1. Declaring Multiple Methods
        2. Using Built-In JSP Methods
        3. Recursion
        4. Scope
        5. Passing Objects to Methods
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Exercises
    4. 4. Reading Data from Web Pages: Buttons and Text Fields
      1. HTML Controls
      2. HTML Forms
        1. The ACTION Attribute
        2. The METHOD Attribute
        3. The TARGET Attribute
      3. Submitting Forms
      4. Sending Data to the Server
      5. Using request Objects
      6. Reading Data on the Server
      7. Using Text Fields
      8. Using Text Areas
      9. Using Password Controls
      10. Using Hidden Controls
      11. Using Buttons
      12. JSP to JSP—Navigating Directly to JSP Pages
      13. Using Multiple Forms
      14. Summary
      15. Q&A
      16. Workshop
        1. Quiz
        2. Exercises
    5. 5. Reading Data from Web Pages: Check Boxes, Radio Buttons, and Select Controls
      1. Using Check Boxes
      2. Using Radio Buttons
      3. Selecting and Deselecting Check Boxes and Radio Buttons in Code
      4. Using Select Controls
      5. Using Multiple-Selection Select Controls
      6. Check Box Groups
      7. Uploading Files
      8. Image Controls
      9. Using <jsp:forward>
      10. Creating Image Maps
      11. Using Named Targets
      12. Getting All Parameter Names
      13. Getting Request Header Information
      14. Reset Buttons
      15. Summary
      16. Q&A
      17. Workshop
        1. Quiz
        2. Exercises
    6. 6. Creating JSP Components: JavaBeans
      1. Creating a Java Class
      2. Creating a Constructor
      3. Creating a Method
      4. Compiling a Java Class
      5. Installing a Compiled Java Class
      6. Using a Compiled Java Class
      7. Creating a Package
      8. Using a Package
      9. Passing Data to a Constructor
      10. Using <jsp:useBean>
      11. Creating a Read-Only Property
      12. <jsp:getProperty>: Getting a Property Value
      13. Creating a Read/Write Property
      14. <jsp:setProperty>: Setting Property Values
      15. Creating Private Methods
      16. Java Utility Classes: Working with Dates
      17. Summary
      18. Q&A
      19. Workshop
        1. Quiz
        2. Exercises
    7. 7. Tracking Users with Sessions and Cookies
      1. Using Hidden Controls
      2. The Cookie Class
      3. The HttpServletResponse Interface
      4. Creating a Cookie
      5. Reading a Cookie
      6. Setting and Reading a Cookie in the Same Page
      7. Using Sessions
      8. Creating a Session
      9. Setting Session Timeouts
      10. Using Applications
      11. Using Sessions, Applications, and JavaBeans
      12. Summary
      13. Q&A
      14. Workshop
        1. Quiz
        2. Exercises
  7. 2. At a Glance
    1. 8. Handling Errors
      1. Syntax Errors
      2. Runtime Errors
      3. Exceptions
      4. Using try/catch Blocks
      5. Handling Specific Exception Types
      6. Catching Multiple Exceptions
      7. Nesting try/catch Statements
      8. Throwing Exceptions Yourself
      9. Throwing Exceptions from Methods
      10. Creating a Custom Exception Object
      11. Printing to the Server Console
      12. Using Log Files
      13. Using JSP Error Pages
      14. Using JSP Error Pages: The exception Object
      15. Using JSP Error Pages: request Object Attributes
      16. Summary
      17. Q&A
      18. Workshop
        1. Quiz
        2. Exercises
    2. 9. Using Custom JSP Tags
      1. Tag Libraries
      2. The Request Tag Library
        1. Downloading the Request Tag Library
        2. Installing the Request Tag Library
        3. Using a Tag Library
        4. HTTP Headers
        5. Request Parameters
        6. Setting Attributes
        7. Request Properties
      3. Tag Library Descriptor Files
      4. Creating a Simple Tag
        1. Creating the Tag Descriptor Library File
        2. Creating the Java Support
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    3. 10. Creating Custom Tags
      1. A Simple Text-Inserting Tag
        1. Creating a TLD File
        2. Creating the Java Support
        3. Working with the Page Context
      2. Creating and Supporting Attributes in Custom Tags
      3. Iterating Tags
      4. Cooperating Tags
      5. Using Scripting Variables
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Exercises
    4. 11. Creating More Powerful JavaBeans
      1. Why Inheritance?
      2. Using Inheritance
      3. Using Access Specifiers
      4. Calling Superclass Constructors
      5. Overloading Methods
      6. Overriding Methods
      7. Using Superclass Variables with Subclassed Objects
      8. Using Runtime Polymorphism
      9. Creating Abstract Classes
      10. Using Interfaces for Multiple Inheritance
      11. Summary
      12. Q&A
      13. Workshop
        1. Quiz
        2. Exercises
    5. 12. Creating Servlets
      1. Using <jsp:plugin>
      2. Creating Java Servlets
      3. Using the response Object
      4. Using the request Object
      5. The Life Cycle of a Servlet
      6. Creating Web Applications with Servlets
      7. Using Servlet Configurations and Contexts
      8. Summary
      9. Q&A
      10. Workshop
        1. Quiz
        2. Exercises
    6. 13. Creating More Powerful Servlets
      1. Sessions and Servlet Contexts
      2. Forwarding Requests to Other Web Resources
      3. Including Output from Other Web Resources
      4. Handling Forwards and Includes
      5. JSP Model 1 Architecture
      6. JSP Model 2 Architecture
        1. The Controller
        2. The Model
        3. The View
      7. Supporting User Authentication
      8. Using Cookies in Servlets
      9. Thread Safety
      10. Summary
      11. Q&A
      12. Workshop
        1. Quiz
        2. Exercises
    7. 14. Using Filters
      1. Understanding Filters
      2. Creating a Simple Filter
      3. Filtering JSP Pages
      4. Creating Filter Chains
      5. Using Initialization Parameters
      6. Inserting Text Into the Response
      7. Logging
      8. Using Filters for User Authentication
      9. Restricting Access
      10. Summary
      11. Q&A
      12. Workshop
        1. Quiz
        2. Exercises
  8. 3. At a Glance
    1. 15. Handling Files on the Server
      1. Using Tomcat Logging
      2. Using the File Class
      3. Streams, Readers, and Writers
      4. Working with Text Files
      5. Writing Text Files
      6. Reading Text Files
      7. Working with Binary Files
      8. Writing Byte Data to Files
      9. Reading Byte Data from Files
      10. Writing Objects to Files
      11. Reading Objects from Files
      12. Summary
      13. Q&A
      14. Workshop
        1. Quiz
        2. Exercises
    2. 16. Getting Started with Databases
      1. What Are Databases?
      2. Java Database Connectivity
      3. JDBC Drivers
        1. Registering an ODBC Source
      4. Creating Connections
      5. Using Structured Query Language
        1. Using the SELECT Statement
        2. Using WHERE Clauses
        3. Using the BETWEEN Clause
        4. Using the IN Clause
        5. Using the LIKE Clause
        6. Using Logical Operations
        7. Using the ORDER BY Clause
        8. Using the AS Clause
        9. Using the DISTINCT Clause
        10. Using Built-in Functions
        11. Using the GROUP BY Clause
        12. Using the DELETE Statement
        13. Using the UPDATE Statement
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Exercises
    3. 17. Working with Databases
      1. Data Lookup
      2. The ResultSet Methods
      3. Navigating in a RecordSet
      4. Setting Cursor Type
      5. Getting Information About a Result Set
      6. Joining Tables
      7. Creating and Filling Tables
      8. Updating Data
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Exercises
    4. 18. Using XML and XSLT in JSP
      1. Handling XML with JSP
      2. Sending XML Data to the Browser
      3. Java and XML
      4. DOM XML Parsing
      5. SAX XML Parsing
      6. Handling XSLT with JSP
      7. Summary
      8. Q&A
      9. Workshop
        1. Quiz
        2. Exercises
    5. 19. Using Struts
      1. Putting Struts to Work
      2. Getting Struts
      3. Creating the View
      4. Creating Custom Tags to Supply Data
      5. Creating the Controller Adapter
      6. Accessing Data in the Results Page
      7. Creating the Model
      8. Summary
      9. Q&A
      10. Workshop
        1. Quiz
        2. Exercises
    6. 20. Creating Images on the Fly and Handling Internet Programming
      1. Creating an Image on the Fly
      2. Drawing in Response to User Actions
      3. Drawing Images
      4. Embedding Images in HTML Documents
      5. Internet Programming—Interacting Directly with Servers
      6. Creating Client/Server Applications
        1. Creating the Client JSP Application
        2. Creating the Server
      7. Summary
      8. Q&A
      9. Workshop
        1. Quiz
        2. Exercises
    7. 21. Client-Side Programming and Deploying Your Projects
      1. Working with JavaScript
        1. Verifying User Data with JavaScript
        2. Accessing Data in HTML Controls with JavaScript
      2. Creating a <SCRIPT> Element
      3. Creating Variables
      4. JavaScript Operators
      5. Branching Statements
      6. JavaScript Loops
      7. JavaScript Functions
      8. Handling Events in JavaScript
        1. Other JavaScript Resources
      9. Deploying Your Web Applications
        1. Copying Files to Deploy Them
        2. Creating WAR Files
        3. Creating JAR Files
      10. Summary
      11. Q&A
      12. Workshop
        1. Quiz
        2. Exercises
  9. Appendixes At a Glance
    1. A. Answers to the Quiz Questions
      1. Quiz Answers for Day 1
      2. Quiz Answers for Day 2
      3. Quiz Answers for Day 3
      4. Quiz Answers for Day 4
      5. Quiz Answers for Day 5
      6. Quiz Answers for Day 6
      7. Quiz Answers for Day 7
      8. Quiz Answers for Day 8
      9. Quiz Answers for Day 9
      10. Quiz Answers for Day 10
      11. Quiz Answers for Day 11
      12. Quiz Answers for Day 12
      13. Quiz Answers for Day 13
      14. Quiz Answers for Day 14
      15. Quiz Answers for Day 15
      16. Quiz Answers for Day 16
      17. Quiz Answers for Day 17
      18. Quiz Answers for Day 18
      19. Quiz Answers for Day 19
      20. Quiz Answers for Day 20
      21. Quiz Answers for Day 21