1. Trang chủ
  2. » Công Nghệ Thông Tin

Core serverlets and java server

514 76 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 514
Dung lượng 3,86 MB

Nội dung

This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] • Table of Contents Core Servlets and JavaServer Pages™: Volume 1: Core Technologies, 2nd Edition By Marty Hall, Larry Brown Publisher: Prentice Hall PTR Pub Date: August 26, 2003 ISBN: 0-13-009229-0 Pages: 736 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 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 [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] • Table of Contents Core Servlets and JavaServer Pages™: Volume 1: Core Technologies, 2nd Edition By Marty Hall, Larry Brown Publisher: Prentice Hall PTR Pub Date: August 26, 2003 ISBN: 0-13-009229-0 Pages: 736 Copyright Acknowledgments About the Authors Introduction Who Should Read This Book Volume Distinctive Features How This Book Is Organized Conventions About the Web Site Chapter An Overview of Servlet and JSP Technology Section 1.1 A Servlet's Job Section 1.2 Why Build Web Pages Dynamically? Section 1.3 A Quick Peek at Servlet Code Section 1.4 The Advantages of Servlets Over "Traditional" CGI Section 1.5 The Role of JSP Part I Servlet Technology Chapter Server Setup and Configuration Section 2.1 Download and Install the Java Software Development Kit (SDK) Section 2.2 Download a Server for Your Desktop Section 2.3 Configure the Server Section 2.4 Configuring Apache Tomcat Section 2.5 Configuring Macromedia JRun Section 2.6 Configuring Caucho Resin Section 2.7 Set Up Your Development Environment Section 2.8 Test Your Setup Section 2.9 Establish a Simplified Deployment Method Section 2.10 Deployment Directories for Default Web Application: Summary This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Section 2.11 Web Applications: A Preview Chapter Servlet Basics Section 3.1 Basic Servlet Structure Section 3.2 A Servlet That Generates Plain Text Section 3.3 A Servlet That Generates HTML Section 3.4 Servlet Packaging Section 3.5 Simple HTML-Building Utilities Section 3.6 The Servlet Life Cycle Section 3.7 The SingleThreadModel Interface Section 3.8 Servlet Debugging Chapter Handling the Client Request: Form Data Section 4.1 The Role of Form Data Section 4.2 Reading Form Data from Servlets Section 4.3 Example: Reading Three Parameters Section 4.4 Example: Reading All Parameters Section 4.5 Using Default Values When Parameters Are Missing or Malformed Section 4.6 Filtering Strings for HTML-Specific Characters Section 4.7 Automatically Populating Java Objects from Request Parameters: Form Beans Section 4.8 Redisplaying the Input Form When Parameters Are Missing or Malformed Chapter Handling the Client Request: HTTP Request Headers Section 5.1 Reading Request Headers Section 5.2 Making a Table of All Request Headers Section 5.3 Understanding HTTP 1.1 Request Headers Section 5.4 Sending Compressed Web Pages Section 5.5 Differentiating Among Different Browser Types Section 5.6 Changing the Page According to How the User Got There Section 5.7 Accessing the Standard CGI Variables Chapter Generating the Server Response: HTTP Status Codes Section 6.1 Specifying Status Codes Section 6.2 HTTP 1.1 Status Codes Section 6.3 A Servlet That Redirects Users to Browser-Specific Pages Section 6.4 A Front End to Various Search Engines Chapter Generating the Server Response: HTTP Response Headers Section 7.1 Setting Response Headers from Servlets Section 7.2 Understanding HTTP 1.1 Response Headers Section 7.3 Building Excel Spreadsheets Section 7.4 Persistent Servlet State and Auto-Reloading Pages Section 7.5 Using Servlets to Generate JPEG Images Chapter Handling Cookies Section 8.1 Benefits of Cookies Section 8.2 Some Problems with Cookies Section 8.3 Deleting Cookies Section 8.4 Sending and Receiving Cookies Section 8.5 Using Cookies to Detect First-Time Visitors Section 8.6 Using Cookie Attributes Section 8.7 Differentiating Session Cookies from Persistent Cookies Section 8.8 Basic Cookie Utilities Section 8.9 Putting the Cookie Utilities into Practice Section 8.10 Modifying Cookie Values: Tracking User Access Counts Section 8.11 Using Cookies to Remember User Preferences Chapter Session Tracking Section 9.1 The Need for Session Tracking Section 9.2 Session Tracking Basics This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Section 9.2 Session Tracking Basics Section 9.3 The Session-Tracking API Section 9.4 Browser Sessions vs Server Sessions Section 9.5 Encoding URLs Sent to the Client Section 9.6 A Servlet That Shows Per-Client Access Counts Section 9.7 Accumulating a List of User Data Section 9.8 An Online Store with a Shopping Cart and Session Tracking Part II JSP Technology Chapter 10 Overview of JSP Technology Section 10.1 The Need for JSP Section 10.2 Benefits of JSP Section 10.3 Advantages of JSP Over Competing Technologies Section 10.4 Misconceptions About JSP Section 10.5 Installation of JSP Pages Section 10.6 Basic Syntax Chapter 11 Invoking Java Code with JSP Scripting Elements Section 11.1 Creating Template Text Section 11.2 Invoking Java Code from JSP Section 11.3 Limiting the Amount of Java Code in JSP Pages Section 11.4 Using JSP Expressions Section 11.5 Example: JSP Expressions Section 11.6 Comparing Servlets to JSP Pages Section 11.7 Writing Scriptlets Section 11.8 Scriptlet Example Section 11.9 Using Scriptlets to Make Parts of the JSP Page Conditional Section 11.10 Using Declarations Section 11.11 Declaration Example Section 11.12 Using Predefined Variables Section 11.13 Comparing JSP Expressions, Scriptlets, and Declarations Chapter 12 Controlling the Structure of Generated Servlets: The JSP page Directive Section 12.1 The import Attribute Section 12.2 The contentType and pageEncoding Attributes Section 12.3 Conditionally Generating Excel Spreadsheets Section 12.4 The session Attribute Section 12.5 The isELIgnored Attribute Section 12.6 The buffer and autoFlush Attributes Section 12.7 The info Attribute Section 12.8 The errorPage and isErrorPage Attributes Section 12.9 The isThreadSafe Attribute Section 12.10 The extends Attribute Section 12.11 The language Attribute Section 12.12 XML Syntax for Directives Chapter 13 Including Files and Applets in JSP Pages Section 13.1 Including Pages at Request Time: The jsp:include Action Section 13.2 Including Files at Page Translation Time: The include Directive Section 13.3 Forwarding Requests with jsp:forward Section 13.4 Including Applets for the Java Plug-In Chapter 14 Using JavaBeans Components in JSP Documents Section 14.1 Why Use Beans? Section 14.2 What Are Beans? Section 14.3 Using Beans: Basic Tasks Section 14.4 Example: StringBean Section 14.5 Setting Bean Properties: Advanced Techniques This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Section 14.6 Sharing Beans Section 14.7 Sharing Beans in Four Different Ways: An Example Chapter 15 Integrating Servlets and JSP: The Model View Controller (MVC) Architecture Section 15.1 Understanding the Need for MVC Section 15.2 Implementing MVC with RequestDispatcher Section 15.3 Summarizing MVC Code Section 15.4 Interpreting Relative URLs in the Destination Page Section 15.5 Applying MVC: Bank Account Balances Section 15.6 Comparing the Three Data-Sharing Approaches Section 15.7 Forwarding Requests from JSP Pages Section 15.8 Including Pages Chapter 16 Simplifying Access to Java Code: The JSP 2.0 Expression Language Section 16.1 Motivating EL Usage Section 16.2 Invoking the Expression Language Section 16.3 Preventing Expression Language Evaluation Section 16.4 Preventing Use of Standard Scripting Elements Section 16.5 Accessing Scoped Variables Section 16.6 Accessing Bean Properties Section 16.7 Accessing Collections Section 16.8 Referencing Implicit Objects Section 16.9 Using Expression Language Operators Section 16.10 Evaluating Expressions Conditionally Section 16.11 Previewing Other Expression Language Capabilities Part III Supporting Technology Chapter 17 Accessing Databases with JDBC Section 17.1 Using JDBC in General Section 17.2 Basic JDBC Examples Section 17.3 Simplifying Database Access with JDBC Utilities Section 17.4 Using Prepared Statements Section 17.5 Creating Callable Statements Section 17.6 Using Database Transactions Section 17.7 Mapping Data to Objects by Using ORM Frameworks Chapter 18 Configuring MS Access, MySQL, and Oracle9i Section 18.1 Configuring Microsoft Access for Use with JDBC Section 18.2 Installing and Configuring MySQL Section 18.3 Installing and Configuring Oracle9i Database Section 18.4 Testing Your Database Through a JDBC Connection Section 18.5 Setting Up the music Table Chapter 19 Creating and Processing HTML Forms Default Web Application: Tomcat Default Web Application: JRun Default Web Application: Resin Section 19.1 How HTML Forms Transmit Data Section 19.2 The FORM Element Section 19.3 Text Controls Section 19.4 Push Buttons Section 19.5 Check Boxes and Radio Buttons Section 19.6 Combo Boxes and List Boxes Section 19.7 File Upload Controls Section 19.8 Server-Side Image Maps Section 19.9 Hidden Fields Section 19.10 Groups of Controls Section 19.11 Tab Order Control This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com Section 19.11 Tab Order Control Section 19.12 A Debugging Web Server Appendix Server Organization and Structure Tomcat JRun Resin [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] Copyright © 2004 Sun Microsystems, Inc.— Printed in the United States of America 4150 Network Circle, Santa Clara, California 95054 U.S.A All rights reserved This product and related documentation are protected by copyright and distributed under licenses restricting its use, copying, distribution, and decompilation No part of this product or related documentation may be reproduced in any form by any means without prior written authorization of Sun and its licensors, if any RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the United States Government is subject to the restrictions set forth in DFARS 252.227-7013 (c)(1)(ii) and FAR 52.227-19 The products described may be protected by one or more U.S patents, foreign patents, or pending applications TRADEMARKS—HotJava, Java, Java Development Kit, Solaris, SPARC, and Sunsoft are trademarks of Sun Microsystems, Inc All other products or services mentioned in this book are the trademarks or service marks of their respective companies or organizations The publisher offers discounts on this book when ordered in bulk quantities For more information, contact: Corporate Sales Department, Phone: 800-382-3419; Fax: 201-236-7141; E-mail: corpsales@prenhall.com; or write: Prentice Hall PTR, Corp Sales Dept., One Lake Street, Upper Saddle River, NJ 07458 Production Editor and Compositor: Vanessa Moore Copy Editor: Mary Lou Nohr Full-Service Production Manager: Anne R Garcia Executive Editor: Gregory G Doench Editorial Assistant: Brandt Kenna Cover Design Director: Jerry Votta Cover Designer: Design Source Art Director: Gail Cocker-Bogusz Manufacturing Manager: Alexis R Heydt-Long Marketing Manager: Debby vanDijk Sun Microsystems Press Publisher: Myrna Rivera 10 Sun Microsystems Press A Prentice Hall Title [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] Acknowledgments Many people helped us with this book Without their assistance, we would still be on the third chapter Brian Baldwin (Atlantic Coast Telesys), John Guthrie (Psynapse Technologies), Randal Hanford (Boeing, University of Washington), Martha McNeil (JHU), Rich Slywczak (NASA), and Dan Unger (Eagle Design and Management) provided valuable technical feedback on many different chapters Their recommendations improved the book considerably Others providing useful suggestions or corrections include Evan Atkinson, Abhay Bakshi, Eliezer Bulka, Joe Bunag, Bob Caviness, Brian Deitte, Pete Fritsch, David Geary, Darryn Graham, Peter Gray, Vladimir Gubanov, Kalman Hazins, Lis Immer, Mike Jenkins, George Jensen, Lian Jin, Rob King, Ashley Lan, Christian Malone, Doug Parker, Ann Platoff, Alexander Pyle, Patrick Quinn-O'Brien, Mark Roth, Hong Sung, Frank Tanner, Jeff Thorn, Alex Turetsky, Kris Uebersax, and Elissa Weidaw I hope I learned from their advice Mary Lou "Eagle Eyes" Nohr spotted my errant commas, awkward sentences, typographical errors, and grammatical inconsistencies She improved the result immensely Vanessa Moore designed the book layout and produced the final version; she did a great job despite my many last-minute changes Greg Doench of Prentice Hall believed in the concept from before the first edition and encouraged us to write a second edition Thanks to all Most of all, Marty would like to thank B.J., Lindsay, and Nathan for their patience and encouragement, and Larry would like to thank Lee for her loving and unfailing support God has blessed us with great families [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] About the Authors 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 You can reach Marty at hall@coreservlets.com; you can find out about his JSP, servlet, and general Java training courses at http://courses.coreservlets.com/ 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 You can reach Larry at brown@coreservlets.com [ Team LiB ] This document is created with a trial version of CHM2PDF Pilot http://www.colorpilot.com [ Team LiB ] Introduction 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 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 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 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 opensource 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 [ Team LiB ] ... Servlets 2.3 and JSP 1.2 (standalone servers) Java 1.2 or later J2EE 1.3 (which includes servlets 2.3 and JSP 1.2) Java 1.3 or later Servlets 2.4 and JSP 2.0 (standalone servers) Java 1.3 or later... Servlets and JavaServer Pages, and the first and second editions of Core Web Programming You can reach Marty at hall@coreservlets.com; you can find out about his JSP, servlet, and general Java training... includes servlets 2.4 and JSP 2.0) Java 1.4 or later We use Java 1.4 in our examples For Solaris, Windows, and Linux, obtain Java 1.4 at http:/ /java. sun.com/j2se/1.4/ and 1.3 at http:/ /java. sun.com/j2se/1.3/

Ngày đăng: 26/03/2019, 11:13

w