Tài liệu Beginning JSP, JSF and Tomcat Java Web Development docx

426 651 4
Tài liệu Beginning JSP, JSF and Tomcat Java Web Development docx

Đ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

Beginning JSP, JSF and Tomcat Java Web Development  Giulio Zambon www.it-ebooks.info ii Beginning JSP, JSF and Tomcat Copyright © 2012 by Giulio Zambon This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN 978-1-4302-4623-7 ISBN 978-1-4302-4624-4 (eBook) Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Steve Anglin Developmental Editor: Douglas Pundick, Ralph Moore Technical Reviewer: Boris Minkin, Manuel Joran Elera Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editors: Katie Sullivan Copy Editor: Michael Sandlin Compositor: Bytheway Publishing Services Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source- code. www.it-ebooks.info iii Contents at a Glance  About the Author xiv  About the Technical Reviewers xv  Chapter 1: Introducing JSP and Tomcat 1  Chapter 2: JSP Elements 19  Chapter 3: JSP Application Architectures 49  Chapter 4: JSP in Action 79  Chapter 5: XML and JSP 121  Chapter 6: Databases 159  Chapter 7: JavaServer Faces 2.2 189  Chapter 8: JSF and eshop 231  Chapter 9: Tomcat 259  Chapter 10: eshop* 281  Appendix A: The Web Page 317  Appendix B: SQL Practical Introduction 379  Appendix C: Abbreviations and Acronyms 405  Index 409 www.it-ebooks.info iv Contents  About the Author xiv  About the Technical Reviewers xv  Chapter 1: Introducing JSP and Tomcat 1 Installing Java 3 Java Test 5 Installing Tomcat 6 Simple Tomcat Test 8 What Is JSP? 9 Viewing a JSP Page 10 Hello World! 12 Listing the HTML-Request Parameters 16 Summary 17  Chapter 2: JSP Elements 19 Introduction 19 Scripting Elements and Java 20 Scriptlets 20 Expressions 20 Declarations 21 Data Types and Variables 21 Objects and Arrays 23 Operators, Assignments, and Comparisons 24 www.it-ebooks.info #/.4%.43 v Selections 25 Iterations 26 Implicit Objects 27 The application Object 27 The config Object 30 The exception Object 31 The out Object 32 The pageContext Object 34 The request Object 34 The response Object 43 The session Object 43 Directive Elements 44 The page Directive 44 The include Directive 47 The taglib Directive 47 Summary 47  Chapter 3: JSP Application Architectures 49 The Model 1 Architecture 49 The Model 2 Architecture 50 The E-bookshop Home Page 52 The E-bookshop Servlet 54 More on E-bookshop 57 E-bookshop’s Folder Structure 60 Eclipse 63 Creating a New Web Project 67 Importing a WAR file 69 Eclipse Occasional Bugs 70 www.it-ebooks.info #/.4%.43 vi A Better Online Bookshop 70 Objects and Operations 71 The Customer Interface 72 The E-shop Architecture 73 The Model 73 The Controller 74 The View 76 Summary 77  Chapter 4: JSP in Action 79 JSP Standard Actions 79 Actions: forward, include, and param 79 Action: useBean 82 Actions: setProperty and getProperty 84 Action: text 87 Actions: element, attribute, and body 87 Actions: plugin, params, and fallback 88 Comments and Escape Characters 90 JSP’s Tag Extension Mechanism 90 Bodyless Custom Actions 91 Bodied Custom Actions 95 Tag Files 98 JSTL and EL 103 JSP Expression Language 103 JSP Standard Tag Library 107 The Core Library 109 The i18n Library: Writing Multi-Lingual Applications 112 Summary 119 www.it-ebooks.info #/.4%.43 vii  Chapter 5: XML and JSP 121 The XML Document 122 Defining Your Own XML Documents 123 XML DTDs 124 XML Schemas 124 Validation 132 JSTL-XML and XSL 139 XPath 139 An XPath Example 143 x:parse 145 XSLT: Transformation from One XML Format to Another 146 XSLT: Transformation from XML to HTML 147 XSL Transformation: Browser Side vs. Server Side 148 x:transform and x:param 152 JSP in XML Syntax 153 Summary 157  Chapter 6: Databases 159 MySQL 159 MySQL Test 161 MySQL/Tomcat Test 165 Database Basics 168 SQL Scripts 170 Java API 171 Connecting to the Database 172 Accessing Data 173 Transactions 176 DB Access in E-shop 176 www.it-ebooks.info #/.4%.43 viii What about the XML Syntax? 180 Possible Alternatives to MySQL 184 Summary 187  Chapter 7: JavaServer Faces 2.2 189 The simplef Application 189 An Alternative to <managed-bean> 195 The simplefx and simpleh Applications 195 The JSF Life Cycle 197 Event Handling 199 The JSF Tag Libraries 199 The html Library 200 The core Library 205 The facelet Library 215 The composite Library 224 Summary 229  Chapter 8: JSF and eshop 231 eshopf 231 The Top Menu 232 The Left Menu (part 1) 233 The Shop Manager 235 The Left Menu (part 2) 236 The Checkout Page 237 web.xml 238 Using and Creating Converters 240 Writing the Converter in Java 241 Registering the Converter with the Application 243 Using the Converter 243 www.it-ebooks.info #/.4%.43 ix Using and Creating Validators 243 Built-In Validators 244 Application-Level Validation 245 Custom Validators 246 Validation Methods in Backing Beans 247 Creating Custom Components 248 Component 249 Renderer 251 Tag 253 Inline Renderer 256 faces-config.xml 257 Summary 257  Chapter 9: Tomcat 259 Tomcat’s Architecture and server.xml 259 Context 260 Connector 261 Host 261 Engine 262 Service 262 Server 262 Listener 263 Global Naming Resources 263 Realm 263 Cluster 263 Valve 264 Loader and Manager 264 Directory Structure 264 conf 265 www.it-ebooks.info #/.4%.43 x lib 265 logs 266 webapps 266 work 266 Logging the Requests 267 Tomcat on Port 80 269 Creating a Virtual Host 269 HTTPS 271 Application Deployment 276 Summary 279  Chapter 10: eshop* 281 The eshop Application 281 What Happens When the Application Starts 283 Handling Requests for Book Selection and Book Search 286 Displaying the Book Details 287 Managing the Shopping Cart 288 Accepting an Order 289 Providing the Payment Details 299 The eshopx Application 300 Style Sheet 301 web.xml 302 JSP Documents 303 Custom Tags and TLD 306 The eshopf Application 308 web.xml and context.xml 309 Style Sheet 310 JSP Documents 312 www.it-ebooks.info [...]... www.it-ebooks.info CHAPTER 1   INTRODUCING JSP AND TOMCAT Installing Java Nothing runs without Java, and you need two different Java packages: one is the runtime environment (JRE), which lets you execute Java, and the other is the Java Development Kit (JDK), which lets you compile Java sources into executable classes They are downloadable together from Oracle’s web site Here’s what you need to do: 1 Go... EL 3.0 (JSR 341), and JSF 2.2 (JSR 344) Version 8 is expected in mid-2013 At the time of this writing, Java 7 is only available as part of the JSE (Java Standard Edition) platform The latest version of Java released in the JEE (Java Enterprise Edition) platform is 6 (update 32) The latest version of Tomcat (7.0), supports Servlets 3.0 and JSF 2.1.7 Viewing a JSP Page With JSP, the web page doesn’t actually... INTRODUCING JSP AND TOMCAT Java Test To test the Java installation, you can use the little application shown in Listing 1-1 Listing 1-1 Exec_http .java /* Exec_http .java - Launches a web page * * Usage: Exec_http URL [arg1 [arg2 [ ]]] * where URL is without "http://" * */ import java. io.*; import java. net.*; class Exec_http { public static void main(String[] vargs) throws java. net.MalformedURLException ,java. io.IOException... JSP AND TOMCAT Figure 1-8 Viewing a JSP page The following steps explain how the web server creates the web page: 1 As with a normal page, your browser sends an HTTP request to the web server This doesn’t change with JSP, although the URL probably ends in jsp instead of html or htm 2 The web server is not a normal server, but rather a Java server, with the extensions necessary to identify and handle Java. .. receive the content of the file identified by /path/whatever.html 1 G Zambon, Beginning JSP, JSF and Tomcat © Giulio Zambon 2012 www.it-ebooks.info CHAPTER 1   INTRODUCING JSP AND TOMCAT 2 In reply, the web server sends an HTTP response containing a plain-text HTML page Images and other non-textual components, such as sound and video clips, only appear in the page as references 3 Your browser receives... and displays the lot JavaServer Pages (JSP) is a technology that helps you create such dynamically generated pages by converting script files into executable Java modules; JavaServer Faces (JSF) is a package that facilitates interactivity with the page viewers; and Tomcat is an application that can execute your code and act as a web server for your dynamic pages Everything you need to develop JSP /JSF. .. titled JavaServer Pages 2.1, which effectively aligned JSP and JSF technologies In particular, JSP 2.1 included a Unified EL (UEL) that merged the two versions of EL defined in JSP 2.0 and JSF 1.2 (itself specified as JSR 252) Sun Microsystems includes JSP 2.1 in its Java Platform, Enterprise Edition 5 (Java EE 5), finalized in May 2006 as JSR 244 The latest version of Java is 7 (specified in JSR 342 and. .. introduce you to Java servlets and JSP, and I’ll show you how they work together within Tomcat to generate dynamic web pages But, first of all, I will guide you through the installation of Java and Tomcat: there wouldn’t be much point in looking at code you can’t execute on your PC, would there? You’ll have to install more packages as you progress Do these installations correctly, and you will never need... CHAPTER 1   INTRODUCING JSP AND TOMCAT Figure 1-5 Tomcat' s Service and Native settings Tomcat runs as a Windows service To start it and stop it, you can right-click the Apache Service Manager icon in the notification area of Windows’ toolbar and select the corresponding operation You can also achieve the same result by opening Windows’ Services control panel (and right-clicking the Tomcat entry, as shown... the evolution of JSP, several frameworks to develop web applications became available In 2004, one of them, JavaServer Faces (JSF) , focused on building user interfaces (UIs) and used JSP by default as the underlying scripting language It provided an API, JSP custom tag libraries, and an expression language The Java Community Process (JCP), formed in 1998, released in May 2006 the Java Specification . Beginning JSP, JSF and Tomcat Java Web Development  Giulio Zambon www.it-ebooks.info ii Beginning JSP, JSF and Tomcat. Reviewers xv  Chapter 1: Introducing JSP and Tomcat 1 Installing Java 3 Java Test 5 Installing Tomcat 6 Simple Tomcat Test 8 What Is JSP? 9 Viewing

Ngày đăng: 21/02/2014, 04:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan