Java- Core Servlets and JSP.pdf

(11404 KB) Pobierz
CSAJSP-for-JavaLobby.book
Overview of On-Line Version
We hope you enjoy this PDF version of the international bestseller Core Servlets and
JavaServer Pages (Sun Microsystems Press). If you are interested in purchasing the
book, it is available through most major on-line and brick-and-mortar bookstores.
See http://www.coreservlets.com for details.
Interested in the sequel? Check out More Servlets and JavaServer Pages at
http://www.moreservlets.com.
Looking for servlet and JSP short courses taught by the author?
Visit http://courses.coreservlets.com. Available on-site at your company or at public
venues.
Complete searchable PDF version offered exclusively through the Java Lobby:
http://www.javalobby.org. Join now!
106011576.003.png 106011576.004.png
© Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
Chapter
TABLE OF CONTENTS
Acknowledgments xix
About the Author xx
Introduction xxi
Real Code for Real Programmers xxii
How This Book Is Organized xxiii
Conventions xxvii
About the Web Site xxvii
PART 1
Servlets 2.1 and 2.2 2
CHAPTER 1
Overview of Servlets and JavaServer Pages 4
1.1 Servlets 5
1.2 The Advantages of Servlets Over “Traditional” CGI 7
Efficient 7
Convenient 7
Home page for this book: http://www.coreservlets.com.
Home page for sequel: http://www.moreservlets.com.
Servlet and JSP training courses: http://courses.coreservlets.com.
v
106011576.005.png
© Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
vi
Contents
Powerful 8
Portable 8
Secure 8
Inexpensive 9
1.3 JavaServer Pages 9
1.4 The Advantages of JSP 10
Versus Active Server Pages (ASP) 10
Versus PHP 10
Versus Pure Servlets 11
Versus Server-Side Includes (SSI) 11
Versus JavaScript 11
Versus Static HTML 12
1.5 Installation and Setup 12
Obtain Servlet and JSP Software 12
Bookmark or Install the Servlet and JSP API Documentation 14
Identify the Classes to the Java Compiler 14
Package the Classes 15
Configure the Server 16
Start the Server 17
Compile and Install Your Servlets 18
CHAPTER 2
First Servlets 20
2.1 Basic Servlet Structure 21
2.2 A Simple Servlet Generating Plain Text 23
Compiling and Installing the Servlet 24
Invoking the Servlet 25
2.3 A Servlet That Generates HTML 26
2.4 Packaging Servlets 27
Creating Servlets in Packages 28
Compiling Servlets in Packages 29
Invoking Servlets in Packages 30
2.5 Simple HTML-Building Utilities 31
2.6 The Servlet Life Cycle 34
The init Method 34
The service Method 36
106011576.006.png
© Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
Contents
vii
The doGet, doPost, and doXxx Methods 37
The SingleThreadModel Interface 38
The destroy Method 38
2.7 An Example Using Initialization Parameters 39
2.8 An Example Using Servlet Initialization and Page Modification Dates 44
2.9 Debugging Servlets 50
2.10 WebClient: Talking to Web Servers Interactively 52
WebClient 52
HttpClient 55
NetworkClient 57
SocketUtil 59
CloseableFrame 60
LabeledTextField 61
Interruptible 63
CHAPTER 3
Handling the Client Request: Form Data 64
3.1 The Role of Form Data 65
3.2 Reading Form Data from Servlets 66
3.3 Example: Reading Three Explicit Parameters 67
3.4 Example: Reading All Parameters 70
3.5 A Resumé Posting Service 74
3.6 Filtering Strings for HTML-Specific Characters 87
Code for Filtering 88
Example 89
CHAPTER 4
Handling the Client Request: HTTP Request Headers 92
4.1 Reading Request Headers from Servlets 94
4.2 Printing All Headers 96
4.3 HTTP 1.1 Request Headers 98
4.4 Sending Compressed Web Pages 104
4.5 Restricting Access to Web Pages 107
106011576.001.png
© Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
viii
Contents
CHAPTER 5
Accessing the Standard CGI Variables 114
5.1 Servlet Equivalent of CGI Variables 116
5.2 A Servlet That Shows the CGI Variables 119
CHAPTER 6
Generating the Server Response: HTTP Status Codes 122
6.1 Specifying Status Codes 124
6.2 HTTP 1.1 Status Codes and Their Purpose 126
6.3 A Front End to Various Search Engines 135
CHAPTER 7
Generating the Server Response: HTTP Response Headers 142
7.1 Setting Response Headers from Servlets 143
7.2 HTTP 1.1 Response Headers and Their Meaning 145
7.3 Persistent Servlet State and Auto-Reloading Pages 154
7.4 Using Persistent HTTP Connections 163
7.5 Using Servlets to Generate GIF Images 168
CHAPTER 8
Handling Cookies 178
8.1 Benefits of Cookies 179
Identifying a User During an E-commerce Session 180
Avoiding Username and Password 180
Customizing a Site 180
Focusing Advertising 181
8.2 Some Problems with Cookies 181
8.3 The Servlet Cookie API 183
Creating Cookies 183
Cookie Attributes 183
Placing Cookies in the Response Headers 186
Reading Cookies from the Client 186
8.4 Examples of Setting and Reading Cookies 186
106011576.002.png
Zgłoś jeśli naruszono regulamin