In the Java universe, the interface layer of the ubiquitous model-view-controller (MVC) software design paradigm is handled by either servlets of JavaServer Pages (JSP). The second edition of Core Servlets and JavaServer Pages, like its predecessor, documents these handy technologies fully and practically. Far more than a straight API reference, this book presents examples--complete with code and a listing or screen shot showing results--wherever possible. It's a fantastic strategy for communicating to programmers what they need to do in order to achieve the effects and behaviors they desire. What's new in the second edition? Lots, in terms of its eponymous software development environments: The book covers servlets 2.4 and JSP 2.0. The examples are more refined, too, and more attention is paid to supporting technologies like Web and database servers.
The didactic approach of authors Marty Hall and Larry Brown is recipe-like. They typically begin with a statement of a problem to be solved, then discuss relevant aspects of the servlet or JSP API. A series of code listings follows, and screen shots showing results bring up the rear. The net effect is that it's easy to spot relevant sections in the table of contents, it's clear how implementation works (thanks to the extensive listings) and there's no doubt about what the results are supposed to be. More elaborate examples show how servlets and JSP work in shopping cart, auction, and other applications. This is a significant and worthwhile update to an important Java book. --David Wall
Topics covered: Java servlets 2.4, JavaServer Pages (JSP) 2.0 (including the new expression language), HTML forms, JDBC database connectivity, and the details of how servlets and JSP are implemented in Apache Tomcat, Caucho Resin, and Macromedia JRun.
From Book News, Inc.
Intended for developers familiar with Java, this guide explains the role of servlets and JavaServer Pages (JSP) in creating e-commerce sites, dynamic web sites, and web-enabled services. Topics include handling missing data, manipulating HTTP status codes, redirecting requests, setting JavaBean properties, and accessing databases with JDBC. The second edition has been updated to servlets 2.4 and JSP 2.0.Copyright © 2004 Book News, Inc., Portland, OR
From the Back Cover
The J2EE(TM) Platform has become the technology of choice for developing professional e-commerce applications, interactive Web sites, and Web-enabled applications and services. Servlet and JSP(TM) technology is the foundation of this platform: it provides the link between Web clients and server-side applications. In this 2nd edition of the worldwide bestseller, the authors show you how to apply the latest servlet and JSP capabilities. Unlike other books that treat servlet or JSP technology in isolation, Core Servlets and JavaServer Pages provides a unified treatment, showing you when servlet technology is best, when the JSP approach is preferred, and when (and how) servlets and JSP should work together. Part I provides exhaustive coverage of the servlet 2.4 specification. It starts with server configuration, basic syntax, the servlet life cycle, and use of form data. It moves on to applying HTTP 1.1, cookies, and session tracking. Advanced topics include compressing Web content, incrementally updating results, dynamically generating images, and creating shopping carts. Part II gives an in-depth guide to JSP 2.0. It covers both the "classic" JSP scripting elements and the new JSP 2.0 expression language. It shows how to control the content type, designate error pages, include files, and integrate JavaBeans components. Advanced topics include sharing beans, generating Excel spreadsheets, and dealing with concurrency. Part III covers two key supporting technologies: HTML forms and database access with JDBC(TM). It explains every standard HTML input element and shows how to use appropriate JDBC drivers, perform database queries, process results, and perform updates. Advanced topics include parameterized queries, stored procedures, and transaction control. Design strategies include ways to integrate servlet and JSP technology, best practices for invoking Java code from JSP pages, plans for dealing with missing and malformed data, and application of the MVC architecture. Handy guides walk you through use of three popular servlet and JSP engines (Apache Tomcat, Macromedia JRun, and Caucho Resin) and some of the most widely used database systems (MySQL, Oracle9i, Microsoft Access). Volume 2 of this book covers advanced topics: filters, custom tag libraries, database connection pooling, Web application security, the JSP Standard Tag Library (JSTL), Apache Struts, JavaServer Faces (JSF), JAXB, and more. The same clear step-by-step explanations that made the first edition so popular Completely updated for the latest standards: servlets 2.4 and JSP 2.0 Hundreds of completely portable, fully documented,industrial-strength examples Detailed treatment of Apache Tomcat, Macromedia JRun, and Caucho Resin Online access to all source code, available free for unrestricted use at the author's www.coreservlets.com web site.
About the Author
MARTY HALL is president of coreservlets.com, Inc., a small company that provides training courses and consulting services related to server-side Java technology. He also teaches Java and Web programming in the Johns Hopkins University part-time graduate program in Computer Science, where he directs the Distributed Computing and Web Technology concentration areas. Marty is the author of four books from Prentice Hall and Sun Microsystems Press: the first edition of Core Servlets and JavaServer Pages, More Servlets and JavaServer Pages, and the first and second editions of Core Web Programming. LARRY BROWN is a Senior Network Engineer and Oracle DBA for the U.S. Navy (NSWCCD), where he specializes in developing and deploying network and Web solutions in an enterprise environment. He is also a Computer Science faculty member at the Johns Hopkins University, where he teachers server-side programming, distributed Web programming, and Java user interface development for the part-time graduate program. Larry is the co-author of the second edition of Core Web Programming, also from Prentice Hall and Sun Microsystems Press.
Excerpt. © Reprinted by permission. All rights reserved.
Suppose your company wants to sell products online. You have a database that gives the price and inventory status of each item. But, your database doesn't speak HTTP, the protocol that Web browsers use. Nor does it output HTML, the format Web browsers need. What can you do? Once users know what they want to buy, how do you gather that information? You want to customize your site for visitors' preferences and interests--how? You want to keep track of user's purchases as they shop at your site--what techniques are required to implement this behavior? When your Web site becomes popular, you might want to compress pages to reduce bandwidth. How can you do this without causing your site to fail for the 30% of visitors whose browsers don't support compression? In all these cases, you need a program to act as the intermediary between the browser and some server-side resource. This book is about using the Java platform for this type of program.
"Wait a second," you say. "Didn't you already write a book about that?" Well, yes. In May of 2000, Sun Microsystems Press and Prentice Hall released Marty's second book, Core Servlets and JavaServer Pages. It was successful beyond everyone's wildest expectations, selling approximately 100,000 copies, getting translated into Bulgarian, Chinese simplified script, Chinese traditional script, Czech, French, German, Hebrew, Japanese, Korean, Polish, Russian, and Spanish, and being chosen by Amazon.com as one of the top five computer programming books of 2001. Even better, Marty was swamped with requests for what he really likes doing: teaching training courses for developers in industry. Despite having to decline most of the requests, he was still able to teach servlet and JSP short courses in Australia, Canada, Japan, Puerto Rico, the Philippines, and at dozens of U.S. venues. What fun!
Since then, use of servlets and JSP has continued to grow at a phenomenal rate. The Java 2 Platform has become the technology of choice for developing e-commerce applications, dynamic Web sites, and Web-enabled applications and service. Servlets and JSP continue to be the foundation of this platform--they provide the link between Web clients and server-side applications. Virtually all major Web servers for Windows, Unix (including Linux), MacOS, VMS, and mainframe operating systems now support servlet and JSP technology either natively or by means of a plugin. With only a small amount of configuration, you can run servlets and JSP in Microsoft IIS, the Apache Web Server, IBM WebSphere, BEA WebLogic, Oracle9i AS, and dozens of other servers. Performance of both commercial and open-source servlet and JSP engines has improved significantly.
However, the field continues to evolve rapidly. For example:The official servlet and JSP reference implementation is no longer developed by Sun. Instead, it is Apache Tomcat, an open-source product developed by a team from many different organizations. So, we provide great detail on Tomcat configuration and usage.Except for Tomcat, the servers popular when the book was first released are no longer widely used. So, we cover Macromedia JRun and Caucho Resin instead.Version 2.4 of the servlet specification was released in late 2003. Many APIs have been added or have changed. So, we have upgraded the book to be consistent with these APIs.Version 2.0 of the JSP specification was released (also late 2003). This version lets you use a shorthand expression language to access bean properties and collection elements. So, we cover both "classic" scripting and use of the JSP 2.0 expression language.Two new versions of JDBC have been released, providing many useful new features. So, we explain database access in the context of these new features.MySQL has emerged as a popular free database. So, we explain how to download, configure, and use MySQL (we also cover Oracle9i and Microsoft Access, of course).
Whew. Lots of action in the server-side Java community. Yup; and to reflect this fact, the book has been completely and totally rewritten from top to bottom. Many new capabilities are now covered. Experienced developer Larry Brown was brought in to add his expertise, especially in database applications. Many hard-learned lessons are explained in detail. Many techniques are now approached differently.
The new version provides a thorough and up-to-date introduction to servlet and JSP programming. We hope you find it useful.Who Should Read This Book
This book is aimed at two main groups.
The primary audience is developers who are familiar with the basics of the Java programming language itself but have little or no experience with server-side applications. For you, virtually the entire book should be valuable; with the possible exception of the JSP 2.0 expression language (which is not applicable if you are using a server that is compliant only with JSP 1.2), you are likely to use capabilities from almost every chapter in almost every real-world application.
The second group is composed of people who are familiar with basic servlet and JSP development and want to learn how to make use of the new capabilities we just described. If you are in this category, you can skim many of the chapters, focusing on the capabilities that are new in servlets 2.4, JSP 2.0, or JDBC 3.0.
Although this book is well suited for both experienced servlet and JSP programmers and newcomers to the technology, it assumes that you are familiar with basic Java programming. You don't have to be an expert Java developer, but if you know nothing about the Java programming language, this is not the place to start. After all, servlet and JSP technology is an application of the Java programming language. If you don't know the language, you can't apply it. So, if you know nothing about basic Java development, start with a good introductory book like Thinking in Java, Core Java, or Core Web Programming. Come back here after you are comfortable with at least the basics.Volume 2
This first volume of the book focuses on core technologies: the servlet and JSP capabilities that you are likely to use in almost every real-life project. The second volume focuses on advanced capabilities: features that you use less frequently but that are extremely valuable in complex applications.
These topics include servlet and JSP filters, declarative and programmatic Web application security, custom tag libraries, the JSP Standard Tag Library (JSTL), Apache Struts, JavaServer Faces (JSF), Java Architecture for XML Binding (JAXB), database connection pooling, advanced JDBC features, and use of Ant for deployment.
For information on the release date of Volume 2, please see the book's Web site at <http://www.coreservlets.com/>.Distinctive Features
This book has five important characteristics that set it apart from many other similar-sounding books:Integrated coverage of servlets and JSP. The two technologies are closely related; you should learn and use them together.Real code. Complete, working, documented programs are essential to learning; we provide lots of them.Step-by-step instructions. Complex tasks are broken down into simple steps that are illustrated with real examples.Server configuration and usage details. We supply lots of concrete examples to get you going quickly.Design strategies. We give lots of experience-based tips on best approaches and practices.Integrated Coverage of Servlets and JSP
One of the key philosophies behind Core Servlets and JavaServer Pages is that servlets and JSP should be learned (and used!) together, not separately. After all, they aren't two entirely distinct technologies: JSP is just a different way of writing servlets. If you don't know servlet programming, you can't use servlets when they are a better choice than JSP, you can't use the MVC architecture to integrate servlets and JSP, you can't understand complex JSP constructs, and you can't understand how JSP scripting elements work (since they are really just servlet code). If you don't understand JSP development, you can't use JSP when it is a better option than servlet technology, you can't use the MVC architecture, and you are stuck using print statements even for pages that consist almost entirely of static HTML.
Servlets and JSP go together! Learn them together!Real Code
Sure, small code snippets are useful for introducing concepts. The book has lots of them. But, for you to really understand how to use various techniques, you also need to see the techniques in the context of complete working programs. Not huge programs: just ones that have no missing pieces and thus really run. We provide plenty of such programs, all of them documented and available for unrestricted use at <http://www.coreservlets.com>.Step-by-Step Instructions
When Marty was a Computer Science graduate student (long before Java existed), he had an Algorithms professor who stated in class that he was a believer in step-by-step instructions. Marty was puzzled: wasn't everyone? Not at all. Sure, most instructors explained simple tasks that way, but this professor took even highly theoretical concepts and said "first you do this, then you do that," and so on. The other instructors didn't explain things this way; neither did his textbooks. But, it helped Marty enormously.
If such an approach works even for theoretical subjects, how much more should it work with applied tasks like those described in this book?Server Configuration and Usage Details
When Marty first tried to learn server-side programming, he grabbed a couple of books, the official specifications, and some online papers. Almost without fail, they said something like "since this technology is portable, you need to read your server's documentation to know how to execute servlets or JSP pages." Aargh! He couldn't even get started. After hunting around, he downloaded a server. He wrote some code. How did he compile it? Where did he put it after it was compiled? How did he invoke it? How about some help here?
Servlet and JSP code is portable. The APIs are standardized. But, server structure and organization are not standardized. The directory in which you place your code is different on Tomcat than it is on JRun. You set up Web applications differently with Resin than you do with other servers. These details are important.
Now, we're not saying that this is a book that is specific to any particular server. We're just saying that when a topic requires server-specific knowledge, it is important to say so. Furthermore, specific examples are helpful. So, when we describe a topic that requires server-specific information like the directory in which to place a Web application, we first explain the general pattern that servers tend to follow. Then, we give very specific details for three of the most popular servers that are available without cost for desktop development: Apache Tomcat, Macromedia JRun, and Caucho Resin.Design Strategies
Sure, it is valuable to know what capabilities the APIs provide. And yes, syntax details are important. But, you also need the big picture. When is a certain approach best? Why? What gotchas do you have to watch out for? Servlet and JSP technology is not perfect; how should you design your system to maximize its strengths and minimize its weaknesses? What strategies simplify the long-term maintenance of your projects? What approaches should you avoid?
We're not new to servlet and JSP technology. We've been doing it for years. And, we've gotten feedback from hundreds of readers and students from Marty's training courses. So, we don't just show you how to use individual features; we explain how these features fit into overall system design and highlight best practices and strategies.How This Book Is Organized
This book consists of three parts: servlet technology, JSP technology, and supporting technologies.Part I: Servlet TechnologyDownloading and configuring a free serverSetting up your development environmentDeploying servlets and JSP pages: some optionsOrganizing projects in Web applicationsBuilding basic servletsUnderstanding the servlet life cycleDealing with multithreading problemsDebugging servlets and JSP pagesReading form parametersHandling missing and malformed dataDealing with incomplete form submissionsUsing HTTP request headersCompressing pagesCustomizing pages based on browser types or how users got thereManipulating HTTP status codes and response headersRedirecting requestsBuilding Excel spread sheets with servletsGenerating custom JPEG images from servletsSending incremental updates to the userHandling cookiesRemembering user preferencesTracking sessionsDifferentiating between browser and server sessionsAccumulating user purchasesImplementing shopping cartsPart II: JSP TechnologyUnderstanding the need for JSPEvaluating strategies for invoking Java code from JSP pagesInvoking Java code with classic JSP scripting elementsUsing the predefined JSP variables (implicit objects)Controlling code structure with the page directiveGenerating Excel spread sheets with JSP pagesControlling multithreading behaviorIncluding pages at request timeIncluding pages at compile timeUsing JavaBeans componentsSetting bean properties automaticallySharing beansIntegrating servlets and JSP pages with the MVC architectureUsing RequestDispatcherComparing MVC data-sharing optionsAccessing beans with the JSP 2.0 expression languageUsing uniform syntax to access array elements, List items, and Map entriesUsing expression language operatorsPart III: Supporting TechnologiesAccessing databases with JDBCSimplifying JDBC usageUsing precompiled (parameterized) queriesExecuting stored proceduresControlling transactionsUsing JDO and other object-to-relational mappingsConfiguring Oracle, MySQL, and Microsoft Access for use with JDBCCreating HTML formsSurveying all legal HTML form elementsDebugging forms with a custom Web serverAbout the Web Site
The book has a companion Web site at <http://www.coreservlets.com/>. This free site includes:Documented source code for all examples shown in the book; this code can be downloaded for unrestricted useLinks to all URLs mentioned in the text of the bookUp-to-date download sites for servlet and JSP softwareInformation on book discountsBook additions, updates, and newsAbout the Training Courses
Hands-on JSP and servlet training courses based on the book are also available. These courses are personally developed and taught by the lead author of the book (Marty). Open-enrollment versions based on the first and second volumes are available at public venues; customizable on-site versions can also be taught at your organization. See <http://courses.coreservlets.com/> for details.
Core Servlets and JavaServer Pages: Volume I: Core Technologies FROM OUR EDITORS
The Barnes & Noble Review
Only a few computer books become classics. Marty Hallᄑs Core Servlets and JavaServer Pages (JSP) was one of them. First published in late 2000, it earned virtually overnight recognition as the definitive servlet/JSP guide. Even Microsoft praised it -- not something you often see with a Java book.
Servlet/JSP standards and techniques have progressed since then, however, so itᄑs a pleasure to introduce Core Servlets and JavaServer Pages (JSP), Second Edition.
With some help from Core Web Programming coauthor Larry Brown, Hall has thoroughly updated this book to reflect the latest Servlet 2.4 and JSP 2.0 standards. This new edition also reflects everything Hallᄑs learned over the past few years, as one of the worldᄑs leading Servlet/JSP trainers and consultants.
Yes, if youᄑre wondering, the new edition contains a complete discussion of the important new JavaServer Pages Standard Tag Library. JSTL simplifies the creation of powerful JSP applications by encapsulating core functionality -- no more reinventing the wheel. It also gives developers a powerful expression language to work with. Itᄑs indispensable -- and who better to learn it from?
Like other books in Prentice Hall PTRᄑs Core Series, this book assumes youᄑre a professional, working programmer, and gives you plenty of robust code examples. It also offers deep insight on how these technologies complement each other, when to use each, and how to integrate both.
After thoroughly reviewing the uses of servlet and JSP technology, Hall and Brown walk you step-by-step through server setup and configuration, addressing leading platforms that range from Tomcat and JRun to Caucho Resin, the new, super-quick XML-based servlet/JSP application server.
Next, youᄑll master the meat-and-potatoes tasks associated with servlet development: handling client requests via form data and HTTP request headers; generating server responses via HTTP status codes and response headers; using cookies; and tracking sessions.
The authors then turn to JSP. Youᄑll learn how to invoke Java code with JSP scripting elements; then use JSP page directives to control the structure of generated servlets. Thereᄑs a full chapter on including files and applets in your JSP pages; and another on integrating JavaBeans components with your JSP documents. Many developers will especially appreciate the authorsᄑ introduction to Model View Controller (MVC) architecture, now the de facto standard for separating business logic, presentation, and program flow.
JSP and servlets donᄑt work in isolation; they integrate with a wide range of supporting technologies. So itᄑs great to see that Hall and Brown have added an entire section on these integration issues. They especially focus on database access. Thereᄑs an entire chapter on JDBC, as well as step-by-step coverage of configuring three leading database platforms for use with JSP and servlets: Oracle 9i, MySQL, and even Microsoft Access.
If youᄑre an experienced developer who wants to buy one JSP/servlets book, you canᄑt go wrong with Core Servlets and JavaServer Pages (JSP), Second Edition. Bill Camarda
Bill Camarda is a consultant, writer, and web/multimedia content developer. His 15 books include Special Edition Using Word 2000 and Upgrading & Fixing Networks for Dummies, Second Edition.
FROM THE PUBLISHER
The J2EE Platform has become the technology of choice for developing professional e-commerce applications, interactive Web sites, and Web-enabled applications and services. Servlet and JSP technology is the foundation of this platform: it provides the link between Web clients and server-side applications. In this 2nd edition of the worldwide bestseller, the authors show you how to apply the latest servlet and JSP capabilities. Unlike other books that treat servlet or JSP technology in isolation, Core Servlets and JavaServer Pages provides a unified treatment, showing you when servlet technology is best, when the JSP approach is preferred, and when (and how) servlets and JSP should work together.
Part I provides exhaustive coverage of the servlet 2.4 specification. It starts with server configuration, basic syntax, the servlet life cycle, and use of form data. It moves on to applying HTTP 1.1, cookies, and session tracking. Advanced topics include compressing Web content, incrementally updating results, dynamically generating images, and creating shopping carts.
Part II gives an in-depth guide to JSP 2.0. It covers both the "classic" JSP scripting elements and the new JSP 2.0 expression language. It shows how to control the content type, designate error pages, include files, and integrate JavaBeans components. Advanced topics include sharing beans, generating Excel spreadsheets, and dealing with concurrency.
Part III covers two key supporting technologies: HTML forms and database access with JDBC. It explains every standard HTML input element and shows how to use appropriate JDBC drivers, perform database queries, process results, and perform updates. Advanced topics includeparameterized queries, stored procedures, and transaction control.
Design strategies include ways to integrate servlet and JSP technology, best practices for invoking Java code from JSP pages, plans for dealing with missing and malformed data, and application of the MVC architecture.
Handy guides walk you through use of three popular servlet and JSP engines (Apache Tomcat, Macromedia JRun, and Caucho Resin) and some of the most widely used database systems (MySQL, Oracle9i, Microsoft Access).
Volume 2 of this book covers advanced topics: filters, custom tag libraries, database connection pooling, Web application security, the JSP Standard Tag Library (JSTL), Apache Struts, JavaServer Faces (JSF), JAXB, and more.
The same clear step-by-step explanations that made the first edition so popularCompletely updated for the latest standards: servlets 2.4 and JSP 2.0Hundreds of completely portable, fully documented,industrial-strength examples
SYNOPSIS
Intended for developers familiar with Java, this guide explains the role of servlets and JavaServer Pages (JSP) in creating e-commerce sites, dynamic web sites, and web-enabled services. Topics include handling missing data, manipulating HTTP status codes, redirecting requests, setting JavaBean properties, and accessing databases with JDBC. The second edition has been updated to servlets 2.4 and JSP 2.0. Annotation ©2003 Book News, Inc., Portland, OR