You are previewing Java Servlet Programming, 2nd Edition.
O'Reilly logo
Java Servlet Programming, 2nd Edition

Book Description

Servlets are an exciting and important technology that ties Java to the Web, allowing programmers to write Java programs that create dynamic web content. Java Servlet Programming covers everything Java developers need to know to write effective servlets. It explains the servlet lifecycle, showing how to use servlets to maintain state information effortlessly. It also describes how to serve dynamic web content, including both HTML pages and multimedia data, and explores more advanced topics like integrated session tracking, efficient database connectivity using JDBC, applet-servlet communicaton, interservlet communication, and internationalization. Readers can use the book's numerous real-world examples as the basis for their own servlets. The second edition has been completely updated to cover the new features of Version 2.2 of the Java Servlet API. It introduces chapters on servlet security and advanced communication, and also introduces several popular tools for easier integration of servlet technology with dynamic web pages. These tools include JavaServer Pages (JSP), Tea, XMLC, and the Element Construction Set. In addition to complete coverage of 2.2 specification, Java Servlet programming, 2nd Edition, also contains coverage of the new 2.3 final draft specification.

Table of Contents

  1. Java Servlet Programming, 2nd Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Servlet API 2.2
      1. Servlet API 2.3
    2. Readers of the First Edition
    3. Audience
      1. What You Need to Know
    4. About the Examples
    5. Organization
    6. Conventions Used in This Book
    7. Request for Comments
    8. Acknowledgments
    9. Acknowledgments from the First Edition
  4. 1. Introduction
    1. History of Web Applications
      1. Common Gateway Interface
        1. FastCGI
        2. PerlEx
        3. mod_perl
      2. Other Solutions
        1. Server extension APIs
        2. Server-side JavaScript
        3. Active Server Pages
        4. JavaServer Pages
      3. Java Servlets
    2. Support for Servlets
      1. Standalone Servlet Containers
      2. Add-on Servlet Containers
      3. Embeddable Servlet Containers
      4. Additional Thoughts
    3. The Power of Servlets
      1. Portability
      2. Power
      3. Efficiency and Endurance
      4. Safety
      5. Elegance
      6. Integration
      7. Extensibility and Flexibility
  5. 2. HTTP Servlet Basics
    1. HTTP Basics
      1. Requests, Responses, and Headers
      2. GET and POST
      3. Other Methods
    2. The Servlet API
    3. Page Generation
      1. Writing Hello World
      2. Running Hello World
      3. Handling Form Data
      4. Handling POST Requests
      5. Handling HEAD Requests
    4. Web Applications
      1. The WEB-INF Directory
      2. The Deployment Descriptor
    5. Moving On
  6. 3. The Servlet Lifecycle
    1. The Servlet Alternative
      1. A Single Java Virtual Machine
      2. Instance Persistence
      3. A Simple Counter
      4. A Simple Synchronized Counter
      5. A Holistic Counter
    2. Servlet Reloading
    3. Init and Destroy
      1. A Counter with Init
      2. A Counter with Init and Destroy
    4. Single-Thread Model
    5. Background Processing
    6. Load on Startup
    7. Client-Side Caching
    8. Server-Side Caching
  7. 4. Retrieving Information
    1. The Servlet
      1. Getting a Servlet Init Parameter
      2. Getting Servlet Init Parameter Names
      3. Getting a Servlet’s Name
    2. The Server
      1. Getting Information About the Server
      2. Writing to a Temporary File
      3. Locking a Servlet to a Server
      4. Getting a Context Init Parameter
      5. Determining the Servlet Version
    3. The Client
      1. Getting Information About the Client Machine
      2. Restricting Access
      3. Getting Information About the User
      4. A Personalized Welcome
      5. The Request
      6. Request Parameters
      7. Generating a License Key
      8. Path Information
        1. Getting path information
        2. Ad hoc path translations
        3. Getting the context path
      9. Getting MIME types
      10. Serving Files
      11. Reading from an Abstract Resource
      12. Serving Resources
      13. Serving Resources for Download
      14. Determining What Was Requested
      15. How It Was Requested
      16. Request Headers
        1. Accessing header values
      17. Wading the Input Stream
        1. Handling POST requests using the input stream
        2. Receiving files using the input stream
      18. Extra Attributes
  8. 5. Sending HTML Information
    1. The Structure of a Response
    2. Sending a Normal Response
    3. Using Persistent Connections
    4. Response Buffering
      1. Controlling the Response Buffer
    5. Status Codes
      1. Setting a Status Code
      2. Improving ViewFile Using Status Codes
    6. HTTP Headers
      1. Setting an HTTP Header
      2. Redirecting a Request
      3. Watching Links to Other Sites
      4. Client Pull
    7. When Things Go Wrong
      1. Status Codes
      2. Configuring Error Pages
      3. Logging
      4. Reporting
      5. Exceptions
        1. ServletException
        2. UnavailableException
      6. Configuring Exception Pages
        1. Knowing when no one’s listening
    8. Six Ways to Skin a Servlet Cat
  9. 6. Sending Multimedia Content
    1. WAP and WML
      1. WML
      2. WAP Device Simulators
      3. Serving WAP Content
      4. Dynamic WAP Content
      5. And That’s a WAP
    2. Images
      1. Image Generation
        1. A “Hello World” image
        2. A dynamically generated chart
      2. Image Composition
        1. Drawing over an image
        2. Combining images
      3. Image Effects
        1. Converting an image to grayscale
        2. Caching a converted image
    3. Compressed Content
    4. Server Push
  10. 7. Session Tracking
    1. User Authentication
    2. Hidden Form Fields
    3. URL Rewriting
    4. Persistent Cookies
      1. Working with Cookies
      2. Shopping Using Persistent Cookies
    5. The Session Tracking API
      1. Session-Tracking Basics
      2. A Hit Count Using Session Tracking
      3. The Session Lifecycle
      4. Setting the Session Timeout
        1. Choosing the right timeout
      5. Lifecycle Methods
      6. Manually Invalidating a Stale Session
      7. How Sessions Really Work
      8. Applet-Based Session Tracking
      9. Noncookie Fallbacks
      10. SessionSnoop
      11. Session Binding Events
      12. Shopping Using Session Tracking
  11. 8. Security
    1. HTTP Authentication
      1. Configuring HTTP Authentication
        1. Role-based authentication
        2. Restricting access to a servlet
      2. Retrieving Authentication Information
    2. Form-Based Authentication
    3. Custom Authentication
      1. Form-Based Custom Authorization
    4. Digital Certificates
    5. Secure Sockets Layer (SSL)
      1. SSL Client Authentication
      2. Configuring SSL Security
      3. Configuring SSL Authentication
      4. Retrieving SSL Authentication Information
  12. 9. Database Connectivity
    1. Relational Databases
    2. The JDBC API
      1. JDBC Drivers
      2. Getting a Connection
      3. Getting a Connection from a Servlet
      4. Executing SQL Queries
      5. Handling SQL Exceptions
      6. Result Sets in Detail
      7. Handling Null Fields
      8. Updating the Database
      9. Using Prepared Statements
    3. Reusing Database Objects
      1. Reusing Database Connections
      2. Reusing Prepared Statements
    4. Transactions
      1. Using Transactions with JDBC
      2. Optimized Transaction Processing
      3. Connection Pooling
      4. Connections as Part of a Session
    5. A Guestbook Servlet
    6. Advanced JDBC Techniques
      1. Stored Procedures
      2. Binaries and Books
    7. Beyond the Core
  13. 10. Applet-Servlet Communication
    1. Communication Options
      1. HTTP and Raw Socket Connections
      2. Servlets and Object Serialization
      3. JDBC, RMI, and a Little CORBA
      4. The Hybrid Approach
    2. Daytime Server
      1. The Applet
      2. Text-Based HTTP Communication
        1. The servlet
        2. Back to the applet
      3. Object-Based HTTP Communication
        1. The servlet
        2. The applet
        3. Posting a serialized object or file
      4. Socket Communication
        1. The servlet
        2. The superclass
        3. The applet
      5. RMI Communication
        1. The servlet
        2. The superclass
        3. The applet
        4. A full-service servlet
    3. Chat Server
      1. The Design
      2. The Servlet
      3. The HTTP Applet
      4. The Socket-Connecting Applet
      5. The RMI Applet
      6. The Dispatcher
  14. 11. Servlet Collaboration
    1. Sharing Information
      1. Sharing with the ServletContext
        1. Using the context to sell burritos
      2. Sharing with Another ServletContext
        1. Class loader issues
    2. Sharing Control
      1. Getting a Request Dispatcher
      2. Dispatching a Forward
        1. Dispatching by name
      3. Forward Versus Redirect
      4. Dispatching an Include
  15. 12. Enterprise Servletsand J2EE
    1. Distributing Load
      1. How to Be Distributable
      2. Many Styles of Distribution
    2. Integrating with J2EE
      1. J2EE Division of Labor
      2. Environment Entries
      3. References to EJB Components
      4. References to External Resource Factories
      5. Servlet Distribution in a J2EE Environment
  16. 13. Internationalization
    1. Western European Languages
      1. HTML Character Entities
      2. Unicode Escapes
    2. Conforming to Local Customs
    3. Non-Western European Languages
      1. Charsets
      2. Writing Encoded Output
      3. Reading and Writing Encoded Output
    4. Multiple Languages
      1. UCS-2 and UTF-8
      2. Writing UTF-8
    5. Dynamic Language Negotiation
      1. Language Preferences
      2. Charset Preferences
      3. Resource Bundles
      4. Writing to Each His Own
      5. The LocaleNegotiator Class
      6. System-Provided Locales
    6. HTML Forms
      1. The Hidden Charset
  17. 14. The Tea Framework
    1. The Tea Language
    2. Getting Started
    3. Request Information
      1. Digging Deeper
    4. Tea Administration
    5. Tea Applications
      1. Text Processing
      2. Content Handling
      3. Request/Response Handling
      4. Writing a Tea Application
    6. A Tool Application
    7. Final Words
  18. 15. WebMacro
    1. The WebMacro Framework
      1. Saying Hello with WebMacro
    2. Installing WebMacro
      1. The WebMacro Template Language
      2. WebMacro Context Tools
    3. WebMacro Directives
      1. #if
      2. #set
      3. #foreach
      4. #parse
      5. #include
      6. #param
      7. #use
    4. WebMacro Templates
      1. A Reusable MacroView Servlet
      2. Template Processing
    5. A Tool Application
    6. Filters
      1. Custom Filters
  19. 16. Element Construction Set
    1. Page Components as Objects
    2. Displaying a Result Set
      1. Customizing the Display
  20. 17. XMLC
    1. A Simple XML Compile
    2. The Manipulation Class
      1. Modifying a List
    3. A Tool Application
  21. 18. JavaServer Pages
    1. Using JavaServer Pages
    2. Behind the Scenes
    3. Expressions and Declarations
    4. Directives
      1. Using Directives
      2. Avoid Java Code in JSP Pages
    5. JSP and JavaBeans
      1. Embedding a Bean
      2. Controlling Bean Properties
      3. Saying “Hello” Using a Bean
    6. Includes and Forwards
    7. A Tool Application
    8. Custom Tag Libraries
      1. Using Custom Tag Libraries
      2. A Tool Application Using Custom Tag Libraries
  22. 19. Odds and Ends
    1. Parsing Parameters
      1. ParameterParser Code
    2. Sending Email
      1. Using the MailMessage Class
      2. Emailing Form Data
    3. Using Regular Expressions
      1. Finding Links with Regular Expressions
    4. Executing Programs
      1. Finger
      2. Executing the finger Command
      3. Executing finger with Arguments
      4. Executing finger with Redirected Output
    5. Using Native Methods
    6. Acting as an RMI Client
    7. Debugging
      1. Check the Logs
      2. Output Extra Information
      3. Use a Standard Debugger
      4. Examine the Client Request
      5. Create a Custom Client Request
      6. Use a Third-Party Tool
      7. Some Final Tips
    8. Performance Tuning
      1. Go Forth, but Don’t Prosper
      2. Don’t Append by Concatenation
      3. Limit Synchronization
      4. Buffer Your Input and Output
      5. Try Using an OutputStream
      6. Use a Profiling Tool
  23. 20. What’s New in the Servlet 2.3 API
    1. Changes in the Servlet API 2.3
      1. Servlets in J2SE and J2EE
      2. Filters
      3. Lifecycle Events
      4. Selecting Character Encodings
      5. JAR Dependencies
      6. Class Loaders
      7. New Error Attributes
      8. New Security Attributes
      9. Little Tweaks
      10. DTD Clarifications
    2. Conclusion
  24. A. Servlet API Quick Reference
    1. GenericServlet
    2. RequestDispatcher
    3. Servlet
    4. ServletConfig
    5. ServletContext
    6. ServletException
    7. ServletInputStream
    8. ServletOutputStream
    9. ServletRequest
    10. ServletResponse
    11. SingleThreadModel
    12. UnavailableException
  25. B. HTTP Servlet API Quick Reference
    1. Cookie
    2. HttpServlet
    3. HttpServletRequest
    4. HttpServletResponse
    5. HttpSession
    6. HttpSessionBindingEvent
    7. HttpSessionBindingListener
    8. HttpSessionContext
    9. HttpUtils
  26. C. Deployment Descriptor DTD Reference
    1. <auth-constraint>
    2. <auth-method>
    3. <context-param>
    4. <description>
    5. <display-name>
    6. <distributable>
    7. <ejb-link>
    8. <ejb-ref>
    9. <ejb-ref-name>
    10. <ejb-ref-type>
    11. <env-entry>
    12. <env-entry-name>
    13. <env-entry-type>
    14. <env-entry-value>
    15. <error-code>
    16. <error-page>
    17. <exception-type>
    18. <extension>
    19. <form-error-page>
    20. <form-login-config>
    21. <form-login-page>
    22. <home>
    23. <http-method>
    24. <icon>
    25. <init-param>
    26. <jsp-file>
    27. <large-icon>
    28. <load-on-startup>
    29. <location>
    30. <login-config>
    31. <mime-mapping>
    32. <mime-type>
    33. <param-name>
    34. <param-value>
    35. <realm-name>
    36. <remote>
    37. <res-auth>
    38. <res-ref-name>
    39. <res-type>
    40. <resource-ref >
    41. <role-link>
    42. <role-name>
    43. <security-constraint>
    44. <security-role>
    45. <security-role-ref >
    46. <servlet>
    47. <servlet-class>
    48. <servlet-mapping>
    49. <servlet-name>
    50. <session-config>
    51. <session-timeout>
    52. <small-icon>
    53. <taglib>
    54. <taglib-location>
    55. <taglib-uri>
    56. <transport-guarantee>
    57. <url-pattern>
    58. <user-data-constraint>
    59. <web-app>
    60. <web-resource-collection>
    61. <web-resource-name>
    62. <welcome-file>
    63. <welcome-file-list>
  27. D. HTTP Status Codes
  28. E. Character Entities
  29. F. Charsets
  30. Index
  31. About the Authors
  32. Colophon
  33. Copyright