Thông tin tài liệu
Java
Servlet Programming
Exploring Java
Java
Threads
Java
Network Programming
Java
Virtual Machine
Java
AWT Reference
Java
Language Reference
Java
Fundamental Classes Reference
Database Programming with JDBC
and Java
Java
Distributed Computing
Developing Java Beans
Java
Security
Java
Cryptography
Java
Swing
Java
Servlet Programming
Also from O’Reilly
Java
in a Nutshell
Java
in a Nutshell, Deluxe Edition
Java
Examples in a Nutshell
Java
Servlet Programming
Jason Hunter
with William Crawford
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
Java ™ Servlet Programming
by Jason Hunter with William Crawford
Copyright © 1998 O’Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472.
Editor:
Paula Ferguson
Production Editor:
Paula Carroll
Editorial and Production Services
:
Benchmark Productions, Inc.
Printing History:
October 1998: First Edition
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered
trademarks and The Java
™
Series is a trademark of O’Reilly & Associates, Inc. The association
of the image of acopper teakettle with the topic of Java
™
Servlet programming is a trademark
of O’Reilly & Associates, Inc. Java
™
and all Java-based trademarks and logos are trademarks
or registered trademarks of Sun Microsystems, Inc., in the United States and other countries.
O’Reilly & Associates, Inc. is independent of Sun Microsystems.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and O’Reilly &
Associates, Inc. was aware of a trademark claim, the designations have been printed in caps
or initial caps.
While every precaution has been taken in the preparation of this book, the publisher assumes
no responsibility for errors or omissions, or for damages resulting from the use of the
information contained herein.
ISBN: 1-56592-391-X [1/00]
[M]
v
Java™ Servlet Programming, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
opd
0: Table of Contents 0:
Preface ix
1. Introduction 1
History of Web Applications 1
Support for Servlets 7
The Power of Servlets 10
2. HTTP Servlet Basics 14
HTTP Basics 14
The Servlet API 17
Page Generation 19
Server-Side Includes 27
Servlet Chaining and Filters 30
JavaServer Pages 37
Moving On 46
3. The Servlet Life Cycle 48
The Servlet Alternative 48
Servlet Reloading 55
Init and Destroy 56
Single-Thread Model 62
Background Processing 64
Last Modified Times 67
vi TABLE OF CONTENTS
Java™ Servlet Programming, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
4. Retrieving Information 70
Initialization Parameters 72
The Server 74
The Client 79
The Request 84
5. Sending HTML Information 124
The Structure of a Response 124
Sending a Normal Response 125
Using Persistent Connections 127
HTML Generation 129
Status Codes 142
HTTP Headers 145
When Things Go Wrong 151
6. Sending Multimedia Content 159
Images 159
Compressed Content 188
Server Push 191
7. Session Tracking 195
User Authorization 196
Hidden Form Fields 197
URL Rewriting 200
Persistent Cookies 202
The Session Tracking API 206
8. Security 221
HTTP Authentication 222
Digital Certificates 232
Secure Sockets Layer (SSL) 234
Running Servlets Securely 237
9. Database Connectivity 242
Relational Databases 243
The JDBC API 246
TABLE OF CONTENTS vii
Java™ Servlet Programming, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Reusing Database Objects 259
Transactions 261
Advanced JDBC Techniques 272
10. Applet-Servlet Communication 277
Communication Options 277
Daytime Server 284
Chat Server 317
11. Interservlet Communication 337
Servlet Manipulation 337
Servlet Reuse 342
Servlet Collaboration 349
Recap 363
12. Internationalization 365
Western European Languages 366
Conforming to Local Customs 369
Non-Western European Languages 371
Multiple Languages 376
Dynamic Language Negotiation 379
HTML Forms 389
Receiving Multilingual Input 395
13. Odds and Ends 397
Parsing Parameters 397
Sending Email 401
Using Regular Expressions 404
Executing Programs 407
Using Native Methods 412
Acting as an RMI Client 413
Debugging 415
Performance Tuning 423
viii TABLE OF CONTENTS
Java™ Servlet Programming, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
A. Servlet API Quick Reference 425
B. HTTP Servlet API Quick Reference 447
C. HTTP Status Codes 472
D. Character Entities 478
E. Charsets 484
Index 487
ix
Java™ Servlet Programming, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
0. Preface 0:
In late 1996, Java on the server side was coming on strong. Several major software
vendors were marketing technologies specifically aimed at helping server-side Java
developers do their jobs more efficiently. Most of these products provided a pre
built infrastructure that could lift the developer’s attention from the raw socket
level into the more productive application level. For example, Netscape intro-
duced something it named “server-side applets”; the World Wide Web Consortium
included extensible modules called “resources” with its Java-based Jigsaw web
server; and with its WebSite server, O’Reilly Software promoted the use of a tech-
nology it (only coincidentally) dubbed “servlets.” The drawback: each of these
technologies was tied to a particular server and designed for very specific tasks.
Then, in early 1997, JavaSoft (a company that has since been reintegrated into Sun
Microsystems as the Java Software division) finalized Java servlets. This action
consolidated the scattered technologies into a single, standard, generic mecha-
nism for developing modular server-side Java code. Servlets were designed to work
with both Java-based and non-Java-based servers. Support for servlets has since
been implemented in nearly every web server, from Apache to Zeus, and in many
non-web servers as well.
Servlets have been quick to gain acceptance because, unlike many new technolo-
gies that must first explain the problem or task they were created to solve, servlets
are a clear solution to a well-recognized and widespread need: generating dynamic
web content. From corporations down to individual web programmers, people
who struggled with the maintenance and performance problems of CGI-based web
programming are turning to servlets for their power, portability, and efficiency.
Others, who were perhaps intimidated by CGI programming’s apparent reliance
on manual HTTP communication and the Perl and C languages, are looking to
servlets as a manageable first step into the world of web programming.
x PREFACE
Java™ Servlet Programming, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
This book explains everything you need to know about Java servlet programming.
The first five chapters cover the basics: what servlets are, what they do, and how
they work. The following eight chapters are where the true meat is—they explore
the things you are likely to do with servlets. You’ll find numerous examples, several
suggestions, a few warnings, and even a couple of true hacks that somehow made it
past technical review.
We cover Version 2.0 of the Servlet API, which was introduced as part of the Java
Web Server 1.1 in December 1997 and clarified by the release of the Java Servlet
Development Kit 2.0 in April 1998. Changes in the API from Version 1.0, finalized
in June 1997, are noted throughout the text.
Audience
Is this book for you? It is if you’re interested in extending the functionality of a
server—such as extending a web server to generate dynamic content. Specifically,
this book was written to help:
CGI programmers
CGI is a popular but somewhat crude method of extending the functionality
of a web server. Servlets provide an elegant, efficient alternative.
NSAPI, ISAPI, ASP, and Server-Side JavaScript programmers
Each of these technologies can be used as a CGI alternative, but each has limi-
tations regarding portability, security, and/or performance. Servlets tend to
excel in each of these areas.
Java applet programmers
It has always been difficult for an applet to talk to a server. Servlets make it
easier by giving the applet an easy-to-connect-to, Java-based agent on the
server.
Authors of web pages with server-side includes
Pages that use server-side includes to call CGI programs can use <SERVLET>
tags to add content more efficiently to a page.
Authors of web pages with different appearances
By this we mean pages that must be available in different languages, have to be
converted for transmission over a low-bandwidth connection, or need to be
modified in some manner before they are sent to the client. Servlets provide
something called servlet chaining that can be used for processing of this type.
Each servlet in a servlet chain knows how to catch, process, and return a
specific kind of content. Thus, servlets can be linked together to do language
translation, change large color images to small black-and-white ones, convert
images in esoteric formats to standard GIF or JPEG images, or nearly anything
else you can think of.
[...]... in the javax .servlet. http package to add HTTPspecific functionality The top-level package name is javax instead of the familiar java, to indicate that the Servlet API is a standard extension Every servlet must implement the javax .servlet. Servlet interface Most servlets implement it by extending one of two special classes: javax servlet GenericServlet or javax .servlet. http.HttpServlet A protocol-independent... the server The Servlet API Now that you have a basic understanding of HTTP, we can move on and talk about the Servlet API that you’ll be using to create HTTP servlets, or any kind of servlets, for that matter Servlets use classes and interfaces from two packages: javax servlet and javax .servlet. http The javax .servlet package contains classes to support generic, protocol-independent servlets These classes... server-side JavaScript is available only with Netscape FastTrack Server and Enterprise Server Version 2.0 and above Java Servlet Programming, eMatter Edition Copyright © 2000 O’Reilly & Associates, Inc All rights reserved 6 CHAPTER 1: INTRODUCTION For more information on programming with server-side JavaScript, see http:// developer.netscape.com/tech/javascript/ssjs/ssjs.html Java Servlets Enter Java servlets... unlike applets, they do not require support for Java in the web browser Java Servlet- based Web Server Main Process Request for Servlet1 JVM Thread Servlet1 Request for Servlet2 Request for Servlet1 Thread Thread Servlet2 Figure 1-4 The servlet life cycle Unlike CGI and FastCGI, which use multiple processes to handle separate programs and/or separate requests, servlets are all handled by separate threads... servlet support A plug-in also can be used with a server that provides a poor or outdated servlet implementation Add-on servlet engines include these: • The Java- Apache project’s JServ module, a freely available servlet engine that adds servlet support to the extremely popular Apache server See http:/ /java apache.org/ • Live Software’s JRun, a freely available plug-in designed to support the full Servlet. .. javax .servlet. http.HttpServlet A protocol-independent servlet should subclass GenericServlet, while an HTTP servlet should subclass HttpServlet, which is itself a subclass of GenericServlet with added HTTP-specific functionality Unlike a regular Java program, and just like an applet, a servlet does not have a main() method Instead, certain methods of a servlet are invoked by the server in Java Servlet Programming, eMatter Edition... To keep things as simple as possible, this servlet just says “Hello World” every time it is accessed via a web browser.* Example 2-1 A servlet that prints “Hello World” import java. io.*; import javax .servlet. *; import javax .servlet. http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html");... Java Virtual Machine (JVM), servlet classes need not be bundled with all JVMs More information about the Java Extension Framework is available at http/ /java. sun.com/ products/jdk/1.2/docs/guide/extensions To make it easy for you to develop servlets, Sun has made publicly available a set of classes that provide basic servlet support The javax .servlet and javax servlet. http packages constitute this Servlet. .. portability Servlets are supported on all platforms that support Java, and servlets work with all the major web servers.* Java servlets, as defined by the Java Software division of Sun Microsystems (formerly known as JavaSoft), are the first standard extension to Java This means that servlets are officially blessed by Sun and are part of the Java language, but they are not part of the core Java API Therefore,... lowestcommon-denominator implementation, so you should pick a servlet engine that has the functionality that you want For a complete, up-to-date list of available servlet engines, see the official list maintained by Sun at: http://jserv .java. sun.com/products /java- server/servlets/environments.html The Power of Servlets So far, we have portrayed servlets as an alternative to other dynamic web content technologies, . Java
Servlet Programming
Exploring Java
Java
Threads
Java
Network Programming
Java
Virtual Machine
Java
AWT Reference
Java
Language. mecha-
nism for developing modular server-side Java code. Servlets were designed to work
with both Java- based and non -Java- based servers. Support for servlets
Ngày đăng: 16/01/2014, 20:20
Xem thêm: Tài liệu O''''Reilly - Java Servlet Programming pptx, Tài liệu O''''Reilly - Java Servlet Programming pptx