INSIDE SERVLETS Server-Side Programming for the Java Platform Dustin R. Callaway TT ADDISON-WESLEY An Imprint of Addison Wesley Longman, Inc. Reading, Massachusetts Harlow, England Menlo Park, California Berkeley, California Don Mills, Ontario Sydney Bonn Amsterdam Tokyo Mexico City
CONTENTS Preface Introduction PART I Introduction to Web Development Chapter I Chapter 2 Networks Protocols TCP/IP Internet Basics Brief History of the Internet Internet Addresses Ports Sockets Name Resolution Firewalls Protocol Tunneling Proxy Servers Internet Standards Summary Web Basics Brief History of the Web Inside URLs Web Browsers Web Servers
VIM Contents Common Gateway Interface 36 Summary 37 Chapter 3 Beyond Web Basics 39 HTTP 39 MIME 54 HTML Forms 56 Summary 64 PART II Introduction to Servlets 67 Chapter 4 Why Servlets? 69 What Is a Servlet? 70 What Can Servlets Do? 74 Why Are Servlets Better Than CGI? 75 Summary 77 Chapter 5 Servlet Basics 79 Basic Servlet Structure 79 Servlet Lifecycle 81 Dissecting a Sample Servlet 82 Summary 91 Chapter 6 Servlet API Basics 93 HttpServlet Class 94 Cenen'cServl et Class 107 ServletRequest Interface 110 HttpServletRequest Interface 118 ServletResponse Interface 124 HttpServletResponse Interface 127 Summary 134 Chapter 7 WritingYour First Servlet 137 Servlet Requirements 137 Main Page 138 View Bulletins Page 145 Post Bulletin Page 152
Contents IX Save Bulletin Page 154 Complete Bulletin Board Servlet 159 Summary 166 Chapter 8 Running Servlets 167 JSDK Servlet Runner 167 Java Web Server 171 JRun 177 ServletExec 184 Summary 189 Chapter 9 Debugging Servlets 191 ServletDebug Class 191 Visual Cafe 198 JBuilder 201 Summary 206 Chapter 10 Beyond Servlet API Basics 209 ServletlnputStream Class 210 ServTetOutputStream Class 210 Se rvl etconf i g Interface 211 ServletContext Interface 213 ServletException Class 215 UnavailableExcepti on Class 216 Summary 217 PART III Advanced Servlet Concepts 219 Chapter 11 Writing Thread Safe Servlets 221 What Is Thread Safety? 221 Synchronization 224 se rvi ce (), doget (), and dopost () Local Variables 225 SingleThreadModel Interface 229 Summary 231 Chapter 12 HTTP Redirects 233 What Is an HTTP Redirect? 233
HTTP Header Syntax for a Redirect 234 Sending a Redirect with the Servlet API 235 Summary 237 Chapter 13 Cookies 239 What Is a Cookie? 239 HTTP Header Syntax for a Cookie 240 Setting Cookies with the Servlet API 243 Summary 248 Chapter 14 State and Session Management 249 What Is a Stateless Protocol? 249 What Is a Session? 250 State and Session Management Defined 253 State and Session Management Methods 254 Session Management with the Servlet API 264 Summary 276 Chapter 15 Server-Side Includes and Request Forwarding 279 Server-Side Includes 279 Using the RequestDi spatcher Object 283 Servlet Chaining 287 Summary 288 Chapter 16 Database Access with JDBC 291 JDBC Architecture 291 Accessing a Database 296 Sample JDBC Servlet 299 Summary 304 PART IV Sample Servlets 307 Chapter 17 Diagnostics Servlet 309 Chapter 18 Form Mailer Servlet 315 Chapter 19 File Upload Servlet 323
Contents XI PARTY Servlet API Quick Reference 335 Chapter 20 javax. servlet Package 337 Interface RequestDispatcher 338 Interface Se rvl et 339 Interface ServletConfig 340 Interface ServletContext 341 Interface ServletRequest 344 Interface ServletResponse 348 Interface Si nglethreadmodel 350 Class GenericServlet 351 Class ServletlnputStream 354 Class ServletOutputStream 355 Class ServletException 357 Class Unavai 1 abl eexcepti on 358 Chapter 21 j avax. servl et. http Package 361 Interface HttpServl etrequest 362 Interface HttpServletResponse 366 Interface HttpSessi on 374 Interface HttpSessionBi ndi nglistener 376 Class Cooki e 377 Class HttpServl et 381 Class HttpSessi onbi ndi ngevent 384 Class HttpUtiIs 385 PART VI Appendices 387 A Common Weil-Known Port Assignments 389 ß Java Port Scanner 390 C The Internet Standardization Process 392 D URL Syntax for Common Protocols 394 Meaning of URL Special Characters 396 F US-ASCII Encoding for Unsafe URL Characters 398
XII Contents G H Java HTTP Server HTTP Response Status Codes 399 405 1 Common HTTP Request Header Fields 408 J K L M BrowserRequest HTTP Viewer Common MIME Types printstacktraceo to String Servlet API Class Hierarchy Diagram 410 414 417 418 Index 421