Core Servlets and JavaServer Pages
Authors: Marty Hall
Venue:  (2000)

From the Book:PREFACE: Preface In early 1996, I started using the Java programming language for the majority of my software development work. I did some CGI programming and even worked a little with the early servlet versions, but for the most part I did desktop and client-side applications. Over the last couple of years, however, there has been a growing emphasis on server-side applications, so I became more serious about servlets and JavaServer Pages. In the past year, there has been a virtual stampede toward the technology among developers, server vendors, and the authors of the Java platform specifications. So much so, in fact, that the technology is rapidly becoming the standard tool for building dynamic Web sites and connecting Web front ends to databases and applications on a server. Unfortunately, however, it was extremely difficult to find good practical advice on servlet and JSP development. I found a number of servlet books, but only a handful of them covered recent versions of the specification, advanced techniques, or reflected real-world experience. The few that did, if they covered JSP at all, hadn't caught up to JSP 1.0, let alone JSP 1.1. Since JSP is a better fit than servlets for many situations, what good was a servlet book that didn't also cover JSP? In the last couple of months, some JSP books have started coming out. But the bulk of them don't cover servlets. What good is that? Since an integral part of JavaServer Pages is the use of scripting elements to create servlet code, you can't do effective JSP development without a thorough understanding of servlets. Besides, most real-world sites don't use just one of the two technologies; theycombinethem both. Finally, as I discovered when I started teaching servlet and JSP development to my students in the Johns Hopkins part-time graduate program (most of whom were professional software developers), few programmers were already comfortable with HTTP 1.1, HTML forms, and JDBC, three critical supporting technologies. Telling them to get a separate book for each of these areas was hardly reasonable: that brought to five the number of books programmers needed if they were going to do serious servlet/JSP development. So, in mid-1999, I put together a short servlet and JSP tutorial with a few dozen examples, put it on the Web, and tried out the material in a couple of my courses. The response was overwhelming. After only a few months, I was getting several thousand visitors a day to the tutorial along with a myriad of requests to expand the coverage of the material. I eventually bowed to the inevitable and started writing. This book is the result. I hope you find it useful. Real Code for Real Programmers This book is aimed at serious software developers. This is not a book that touts the potential of e-commerce or pontificates about how Web-enabled applications will revolutionize your business. Instead, it is a hands-on book aimed at helping programmers who are already convinced of the need for dynamic Web sites get started building them right away. In showing how to build these sites, I try to illustrate the most important approaches and warn you of the most common pitfalls. Along the way, I include plenty of working code: more than a hundred documented Java classes, for instance. I try to give detailed examples of the most important and frequently used features, summarize the lesser-used ones, and refer you to the APIs (available on-line) for a few of the rarely used ones. Nor is this a book that skims dozens of technologies at a high level. Although I don't claim that this is a definitive reference on every technology it touches on (e.g., there are a number of books this size just on JDBC), if the book covers a topic, it does so in enough detail for you to sit down and start writing real programs. The one exception to this rule is the Java programming language itself. Although I don't assume any familiarity with server-side programming, I do expect you to be familiar with the basics of Java language development. If you're not, you will need to pick up a good tutorial like Core Java, Core Web Programming, or Thinking in Java. A word of caution, however. Nobody becomes a great developer just by reading. You have to write some real code, too. The more, the better. In each chapter, I suggest that you start by making a simple program or a small variation of one of the examples given, then strike off on your own with a more significant project. Skim the sections you don't plan on using right away, then come back when you are ready to try them out. If you do this, you should quickly develop the confidence to handle the real-world problems that brought you here in the first place. You should be able to decide where servlets apply well, where JSP is better, and where a combination is best. You should not only know how to generate HTML content, but you should also understand building other media types like GIF images or Excel spreadsheets. You should understand HTTP 1.1 well enough to use its capabilities to enhance the effectiveness of your pages. You should have no qualms about developing Web interfaces to your corporate databases, using either HTML forms or applets as front ends. You should be able to spin off complex behaviors into JavaBeans components or custom JSP tag libraries, then decide when to use these components directly and when to start requests with servlets that set things up for separate presentation pages. You should have fun along the way. You should get a raise. How This Book Is Organized This book is divided into three parts: Servlets, JavaServer Pages, and Supporting Technologies. Part I: Servlets Part I covers servlet development with the 2.1 and 2.2 specifications. Although version 2.2 (along with JSP 1.1) is mandated by the Java 2 Platform, Enterprise Edition (J2EE), many commercial products are still at the earlier releases, so it is important to understand the differences. Also, although servlet code is portable across a huge variety of servers and operating systems, server setup and configuration details are not standardized. So, I include specific details for Apache Tomcat, Sun's JavaServer Web Development Kit (JSWDK), and the Java Web Server. Servlet topics include: When and why you would use servlets Obtaining and configuring the servlet and JSP software The basic structure of servlets The process of compiling, installing, and invoking servlets Generating HTML from servlets The servlet life cycle Page modification dates and browser caches Servlet debugging strategies Reading form data from servlets Handling both GET and POST requests with a single servlet An on-line resume posting service Reading HTTP request headers from servlets The purpose of each of the HTTP 1.1 request headers Reducing download times by compressing pages Restricting access with password-protected servlets The servlet equivalent of each standard CGI variable Using HTTP status codes The meaning of each of the HTTP 1.1 status code values A search engine front end Setting response headers from servlets The purpose of each of the HTTP 1.1 response headers Common MIME types A servlet that uses the Refresh header to repeatedly access ongoing computations Servlets that exploit persistent (keep-alive) HTTP connections Generating GIF images from servlets Cookie purposes and problems The Cookie API Some utilities that simplify cookie handling A customized search engine front end The purposes of session tracking The servlet session tracking API Using sessions to show per-client access counts An on-line store that uses session tracking, shopping carts, and pages automatically built from catalog entries Part II: JavaServer Pages JSP provides a convenient alternative to servlets for pages that mostly consist of fixed content. Part II covers the use of JavaServer Pages version 1.0 and 1.1. JSP topics include: When and why you would use JavaServer Pages How JSP pages are invoked Using JSP expressions, scriptlets, and declarations Predefined variables that can be used within expressions and scriptlets The page directive Designating which classes are imported Specifying the MIME type of the page Generating Excel spreadsheets Controlling threading behavior Participating in sessions Setting the size and behavior of the output buffer Designating pages to process JSP errors XML-compatible syntax for directives Including JSP files at the time the main page is translated into a servlet Including HTML or plain text files at the time the client requests the page Including applets that use the Java Plug-In Using JavaBeans with JSP Creating and accessing beans Setting bean properties explicitly Associating bean properties with input parameters Automatic conversion of bean property types Sharing beans among multiple JSP pages and servlets Creating JSP tag libraries Tag handler classes Tag library descriptor files The JSP taglib directive Simple tags Tags that use attributes Tags that use the body content between their start and end tags Tags that modify their body content Looping tags Nested tags Integrating servlets and JSP Forwarding requests from servlets to static and dynamic resources Using servlets to set up beans for use by JSP pages An on-line travel agency combining servlets and JSP Including JSP output in servlets Forwarding requests from JSP pages Part III: Supporting Technologies Part III covers three topics that are commonly used in conjunction with servlets and JSP: HTML forms, applets talking to servlets, and JDBC. Topics include: Sending data from forms Text controls Push buttons Check boxes and radio buttons Combo boxes and list boxes File upload controls Server-side image maps Hidden fields Grouping controls Tab ordering A Web server for debugging forms Sending GET data from an applet and having the browser display the results Having applets send GET data and process the results themselves (HTTP tunneling) Using object serialization to exchange high-level data structures between applets and servlets Having applets send POST data and process the results themselves Applets bypassing the HTTP server altogether About the Web Site The book has a companion Web site at ...