Java Servlet Programming: Help for Server Side Java Developers

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.

1140203433
Java Servlet Programming: Help for Server Side Java Developers

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.

29.99 In Stock
Java Servlet Programming: Help for Server Side Java Developers

Java Servlet Programming: Help for Server Side Java Developers

Java Servlet Programming: Help for Server Side Java Developers

Java Servlet Programming: Help for Server Side Java Developers

eBook

$29.99  $39.99 Save 25% Current price is $29.99, Original price is $39.99. You Save 25%.

Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

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.


Product Details

ISBN-13: 9781449390679
Publisher: O'Reilly Media, Incorporated
Publication date: 04/03/2001
Series: Java Series
Sold by: Barnes & Noble
Format: eBook
Pages: 782
File size: 5 MB

About the Author


Jason Hunter is a Java consultant, speaker, instructor, and author.Jason graduated summa cum laude from Willamette University (Salem,Oregon) in 1995 with a degree in computer science. After graduation,he worked at Silicon Graphics in Mountain View, California, forseveral years, where he was responsible for developing (and breaking)all sorts of Web technologies. He currently works as the ChiefTechnology Officer of a Silicon Valley start-up, K&A Software, wherehe specializes in Java training and consulting, with an emphasis onservlets. Jason also writes columns for JavaWorld.

Jason began programming in Java in the summer of 1995 and hasconcentrated on servlets and related server-extension technologiessince December 1996. If by some miracle you don''t find him at work,he''s probably out hiking in the mountains.

William "Will" Crawford got involved withWeb development back in 1995. He has worked at the Children''s Hospital Informatics Program in Boston, where he helped develop the first Web-based electronic medical record system and was involved in some of the first uses of Java at the enterprise level. He has consulted on intranet development projects for, among others, Children''s Hospital, Massachusetts General Hospital, Brigham and Women''s Hospital, the Boston Anesthesia Education Foundation, and Harvard Medical Center. Will currently heads the product development team at Invantage, Inc., aCambridge, Massachusetts, startup developing Java-based intranet tools for the pharmaceutical industry. In his spare time, he is an avid amateur photographer, writer, and pursuer of a bachelor''s degree in economics at Yale University.

Read an Excerpt


Chapter 2: HTTP SERVLET BASICS

HTTP Servlet Basics

This chapter provides a quick introduction to some of the things an HTTP servlet can do. For example, an HTTP servlet can generate an HTML page, either when the servlet is accessed explicitly by name, by following a hypertext link, or as the result of a form submission. An HTTP servlet can also be embedded inside an HTML page, where it functions as a server-side include. Servlets can be chained together to produce complex effects-one common use of this technique is for filtering content. Finally, snippets of servlet code can be embedded directly in HTML pages using a new technique called Java Server Pages.

Although the code for each of the examples in this chapter is available for download (as described in the Preface), we would suggest that for these first examples you deny yourself the convenience of the Internet and type in the examples. It should help the concepts seep into your brain.

Don't be alarmed if we seem to skim lightly over some topics in this chapter. Servlets are powerful and, at times, complicated. The point here is to give you a general overview of how things work, before jumping in and overwhelming you with all of the details. By the end of this book, we promise that you'll be able to write servlets that do everything but make tea.

HTTP Basics

Before we can even show you a simple HTTP servlet, we need to make sure that you have a basic understanding of how the protocol behind the Web, HTTP, works. If you're an experienced CGI programmer (or if you've done any serious server-side web programming), you can safely skip this section. Better yet, you might skim it to refresh your memory about the finer points of the GET and POST methods. If you are new to the world of server-side web programming, however, you should read this material carefully, as the rest of the book is going to assume that you understand HTTP. For a more thorough discussion of HTTP and its methods, see Web Client Programming by Clinton Wong (O'Reilly).

Requests, Responses, and Headers

HTTP is a simple, stateless protocol. A client, such as a web browser, makes a request, the web server responds, and the transaction is done. When the client sends a request, the first thing it specifies is an HTTP command, called a method, that tells the server the type of action it wants performed. This first line of the request also specifies the address of a document (a URL) and the version of the HTTP protocol it is using. For example:

intro.html TP/1.

This request uses the GET method to ask for the document named intro html using HTTP Version 1.0. After sending the request, the client can send optional header information to tell the server extra information about the request, such as what software the client is running and what content types it understands. This information doesn't directly pertain to what was requested, but it could be used by the server in generating its response. Here are some sample request headers:

User-Agent: at: Mozilla /4.0 (compatible; MSIE 4, windows 95)
Accept image/gif, image/jpeg, text/*, */*

The User-Agent header provides information about the client software, while the Accept header specifies the media (MIME) types that the client prefers to accept. (We'll talk more about request headers in the context of servlets in Chapter 4, Retrieving Information.) After the headers, the client sends a blank line, to indicate the end of the header section. The client can also send additional data, if appropriate for the method being used, as it is with the, POST method that we'll discuss shortly. If the request doesn't send any data, it ends with an empty line.

After the client sends the request, the server processes it and sends back a response. The first line of the response is a status line that specifies the version of the HTTP protocol the server is using, a status code, and a description of the le. For example:

HTTP/1.0   200
OK

This status line includes a status code of 200, which indicates that the request was successful, hence the description "OK" Another common status code is 404, with the description "Not Found" - as you can guess, this means that the requested document was not found. Chapter 5, Sending HTML Information, discusses common status codes and how you can use them in servlets, while Appendix C, HTTP Status Codes, provides a complete list of HTTP status codes. After the status line, the server sends response headers that tell the client things like what software the server is running and the content type of the server's response. For example:

Date: Saturday, 23-May-98 03:25:12 GMT
server: Javawebserver/1.1.1 MIME-
version: 1.0 Content-type: text/html
content-length: 1029 Last-modified:
Thursday, 7-May-98 12:15:35 GMT

The Server header provides information about the server software, while the content-type header specifies the MIME type of the data included with the response. (We'll also talk more about response headers in Chapter 5.) The server sends a blank line after the headers, to conclude the header section. If the request was successful, the requested data is then sent as part of the response. Otherwise, the response may contain human-readable data that explains why the server couldn't fulfill the request.

GET and POST

When a client connects to a server and makes an HTTP request, the request can be of several different types, called methods. The most frequently used methods are GET and POST. Put simply, the GET method is designed for getting information (a document, a chart, or the results from a database query), while the POST method is designed for posting information (a credit card number, some new chart data, or information that is to be stored in a database). To use, a bulletin board analogy, GET is for reading and POST is for tacking up new material.

The GET method, although it's designed for reading information, can include as part of the request some of its own information that better describes what to get such as an x, y scale for a dynamically created chart. This information is passed as a sequence of characters appended to the request URL in what's called a query string. Placing the extra information in the URL in this way allows the page to be bookmarked or emailed like any other. Because GET requests theoretically shouldn't need to send large amounts of information, some servers limit the length of URLs and query strings to about 240 characters.

The POST method uses a different technique to send information to the server because in some cases it may need to send megabytes of information. A POST request passes all its data, of unlimited length, directly over the socket connection as part of its HTTP request body. The exchange is invisible to the client. The URL doesn't change at all. Consequently, POST requests cannot be bookmarked or emailed or, in some cases, even reloaded. That's by design-information sent to the server. such as your credit card number, should be sent only once....

Table of Contents

1. Introduction

 History of Web Applications

 Support for Servlets

 The Power of Servlets

2. HTTP Servlet Basics

 HTTP Basics

 The Servlet API

 Page Generation

 Web Applications

 Moving On

3. The Servlet Lifecycle

 The Servlet Alternative

 Servlet Reloading

 Init and Destroy

 Single-Thread Model

 Background Processing

 Load on Startup

 Client-Side Caching

 Server-Side Caching

4. Retrieving Information

 The Servlet

 The Server

 The Client

5. Sending HTML Information

 The Structure of a Response

 Sending a Normal Response

 Using Persistent Connections

 Response Buffering

 Status Codes

 HTTP Headers

 When Things Go Wrong

 Six Ways to Skin a Servlet Cat

6. Sending Multimedia Content

 WAP and WML

 Images

 Compressed Content

 Server Push

7. Session Tracking

 User Authentication

 Hidden Form Fields

 URL Rewriting

 Persistent Cookies

 The Session Tracking API

8. Security

 HTTP Authentication

 Form-Based Authentication

 Custom Authentication

 Digital Certificates

 Secure Sockets Layer (SSL)

9. Database Connectivity

 Relational Databases

 The JDBC API

 Reusing Database Objects

 Transactions

 A Guestbook Servlet

 Advanced JDBC Techniques

 Beyond the Core

10. Applet-Servlet Communication

 Communication Options

 Daytime Server

 Chat Server

11. Servlet Collaboration

 Sharing Information

 Sharing Control

12. Enterprise Servlets and J2EE

 Distributing Load

 Integrating with J2EE

13. Internationalization

 Western European Languages

 Conforming to Local Customs

 Non-Western European Languages

 Multiple Languages

 Dynamic Language Negotiation

 HTML Forms

14. The Tea Framework

 The Tea Language

 Getting Started

 Request Information

 Tea Administration

 Tea Applications

 A Tool Application

 Final Words

15. WebMacro

 The WebMacro Framework

 Installing WebMacro

 WebMacro Directives

 WebMacro Templates

 A Tool Application

 Filters

16. Element Construction Set

 Page Components as Objects

 Displaying a Result Set

17. XMLC

 A Simple XML Compile

 The Manipulation Class

 A Tool Application

18. JavaServer Pages

 Using JavaServer Pages

 Behind the Scenes

 Expressions and Declarations

 Directives

 JSP and JavaBeans

 Includes and Forwards

 A Tool Application

 Custom Tag Libraries

19. Odds and Ends

 Parsing Parameters

 Sending Email

 Using Regular Expressions

 Executing Programs

 Using Native Methods

 Acting as an RMI Client

 Debugging

 Performance Tuning

20. What's New in the Servlet 2.3 API

 Changes in the Servlet API 2.3

 Conclusion

A. Servlet API Quick Reference

B. HTTP Servlet API Quick Reference

C. Deployment Descriptor DTD Reference

D. HTTP Status Codes

E. Character Entities

F. Charsets

Index

From the B&N Reads Blog

Customer Reviews