You are previewing Java Servlet Programming.
O'Reilly logo
Java Servlet Programming

Book Description

A few years ago, the hype surrounding applets put Java on the map as a programming language for the Web. Today, Java servlets stand poised to take Java to the next level as a Web development language. The main reason is that servlets offer a fast, powerful, portable replacement for CGI scripts. The Java Servlet API, introduced as the first standard extension to Java, provides a generic mechanism to extend the functionality of any kind of server. Servlets are most commonly used, however, to extend Web servers, performing tasks traditionally handled by CGI programs. Web servers that can support servlets include: Apache, Netscape's FastTrack and Enterprise Servers, Microsoft's IIS, O'Reilly's WebSite, and JavaSoft's Java Web Server. The beauty of servlets is that they execute within the Web server's process space and they persist between invocations. This gives servlets tremendous performance benefits over CGI programs. Yet because they're written in Java, servlets are far less likely to crash a Web server than a C-based NSAPI or ISAPI extension. Servlets have full access to the various Java APIs and to third-party component classes, making them ideal for use in communicating with applets, databases, and RMI servers. Plus, servlets are portable between operating systems and between servers -- with servlets you can "write once, serve everywhere." Java Servlet Programming covers everything you need to know to write effective servlets and includes numerous examples that you can use as the basis for your own servlets. The book explains the servlet life cycle, showing how you can use servlets to maintain state information effortlessly. It also describes how to serve dynamic Web content, including both HTML pages and multimedia data. Finally, it explores more advanced topics like integrated session tracking, efficient database connectivity using JDBC, applet-servlet communication, inter-servlet communication, and internationalization.

Table of Contents

  1. Java Servlet Programming
    1. Preface
      1. Audience
        1. What You Need to Know
      2. About the Examples
      3. Organization
      4. Conventions Used in This Book
      5. Request for Comments
      6. Acknowledgments
        1. Jason Hunter
        2. William Crawford
    2. 1. Introduction
      1. History of Web Applications
        1. Common Gateway Interface
          1. FastCGI
          2. mod_perl
          3. PerlEx
        2. Other Solutions
          1. Server Extension APIs
          2. Active Server Pages
          3. Server-side JavaScript
        3. Java Servlets
      2. Support for Servlets
        1. Standalone Servlet Engines
        2. Add-on Servlet Engines
        3. Embeddable Servlet Engines
        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
    3. 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. Server-Side Includes
        1. Writing a Server-Side Include
      5. Servlet Chaining and Filters
        1. Creating a Servlet Chain
        2. Running Deblink
        3. The Loophole
      6. JavaServer Pages
        1. Using JavaServer Pages
        2. Behind the Scenes
        3. Expressions and Directives
        4. Declarations
        5. JavaServer Pages and JavaBeans
      7. Moving On
    4. 3. The Servlet Life Cycle
      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. Last Modified Times
    5. 4. Retrieving Information
      1. Initialization Parameters
        1. Getting an Init Parameter
        2. Getting Init Parameter Names
      2. The Server
        1. Getting Information About the Server
        2. Locking a Servlet to a Server
      3. The Client
        1. Getting Information About the Client Machine
        2. Restricting Access to the United States and Canada
        3. Getting Information About the User
        4. A Personalized Welcome
      4. The Request
        1. Request Parameters
        2. Generating a License Key
        3. Path Information
          1. Getting path information
          2. Ad hoc path translations
          3. Getting MIME types
        4. Serving Files
        5. Determining What Was Requested
        6. An Improved Counter
        7. How It Was Requested
        8. Request Headers
          1. Accessing header values
          2. Headers in servlet chains
        9. Wading the Input Stream
          1. Chaining servlets using the input stream
          2. Handling POST requests using the input stream
          3. Receiving files using the input stream
        10. Extra Attributes
    6. 5. Sending HTML Information
      1. The Structure of a Response
      2. Sending a Normal Response
      3. Using Persistent Connections
      4. HTML Generation
        1. Generating Hello World
        2. Generating a Weather Forecast
          1. Weather forecast constructed by hand
          2. Weather forecast using HTML generation
          3. Weather forecast using HTML generation creatively
          4. HTML generation and databases
      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. Client Pull
      7. When Things Go Wrong
        1. Status Codes
        2. Logging
        3. Reporting
        4. Exceptions
        5. Knowing When No One’s Listening
    7. 6. Sending Multimedia Content
      1. 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
        4. Image Effects in Filter Chains
        5. An Image of an Embedded Applet
          1. An image of a simple applet
          2. A generic applet viewer
          3. Advantages and disadvantages
      2. Compressed Content
      3. Server Push
    8. 7. Session Tracking
      1. User Authorization
      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 Life Cycle
        4. Manually Invalidating a Stale Session
        5. Putting Sessions in Context
        6. Manually Invalidating All Stale Sessions
        7. Storing Session IDs
        8. Session Snoop
        9. Session Binding Events
        10. Shopping Using Session Tracking
    9. 8. Security
      1. HTTP Authentication
        1. Retrieving Authentication Information
        2. Custom Authorization
        3. Form-based Custom Authorization
      2. Digital Certificates
      3. Secure Sockets Layer (SSL)
        1. SSL Client Authentication
        2. Retrieving SSL Authentication Information
      4. Running Servlets Securely
        1. The Servlet Sandbox
        2. Fine-grained Control
        3. Access Controllers
    10. 9. Database Connectivity
      1. Relational Databases
      2. The JDBC API
        1. JDBC Drivers
        2. Getting a Connection
        3. Executing SQL Queries
        4. Handling SQL Exceptions
        5. Result Sets in Detail
        6. Handling Null Fields
        7. Updating the Database
        8. 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. Advanced JDBC Techniques
        1. Stored Procedures
        2. Binaries and Books
    11. 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
        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
    12. 11. Interservlet Communication
      1. Servlet Manipulation
        1. Viewing the Currently Loaded Servlets
        2. Saving the State of the Currently Loaded Servlets
      2. Servlet Reuse
        1. An Improved getServlet()
        2. Reusing ChatServlet
      3. Servlet Collaboration
        1. Collaboration Through the System Properties List
          1. Using properties to sell burritos
          2. Faster image chaining
        2. Collaboration Through a Shared Object
          1. Using a shared class to sell burritos
          2. Using a servlet as the shared object
        3. Collaboration Through Inheritance
          1. Inheriting a shared reference
          2. Inheriting the shared information
      4. Recap
    13. 12. 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. Future Directions
      6. HTML Forms
        1. The Hidden Charset
      7. Receiving Multilingual Input
    14. 13. Odds and Ends
      1. Parsing Parameters
        1. ParameterParser Code
      2. Sending Email
        1. Using sun.net.smtp.SmtpClient
        2. Emailing Form Data
      3. Using Regular Expressions
        1. Improving Deblink 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. Use a Third-Party Tool
        5. Examine the Client Request
        6. Create a Custom Client Request
        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
    15. A. Servlet API Quick Reference
      1. GenericServlet
      2. Servlet
      3. ServletConfig
      4. ServletContext
      5. ServletException
      6. ServletInputStream
      7. ServletOutputStream
      8. ServletRequest
      9. ServletResponse
      10. SingleThreadModel
      11. UnavailableException
    16. B. HTTP Servlet API Quick Reference
      1. Cookie
      2. HttpServlet
      3. HttpServletRequest
      4. HttpServletResponse
      5. HttpSession
      6. HttpSessionBindingEvent
      7. HttpSessionBindingListener
      8. HttpSessionContext
      9. HttpUtils
    17. C. HTTP Status Codes
    18. D. Character Entities
    19. E. Charsets
    20. Index
    21. Colophon