www.it-ebooks.info www.it-ebooks.info Java EE 7 Essentials Arun Gupta www.it-ebooks.info Java EE 7 Essentials by Arun Gupta Copyright © 2013 Arun Gupta. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editors: Mike Loukides and Meghan Blanchette Indexer: Angela Howard Production Editor: Kara Ebrahim Cover Designer: Randy Comer Copyeditor: Rachel Monaghan Interior Designer: David Futato Proofreader: Linley Dolby Illustrator: Rebecca Demarest August 2013: First Edition Revision History for the First Edition: 2013-08-08: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449370176 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Java EE 7 Essentials, the image of glassfish, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-37017-6 [LSI] www.it-ebooks.info To Menka, the eternal sunshine in my life. You make my days shine and life upbeat. To Aditya and Mihir, your stories and games are invaluable to me. www.it-ebooks.info www.it-ebooks.info Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Java Platform, Enterprise Edition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Introduction 1 Deliverables 3 What’s New in Java EE 7 6 2. Servlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 WebServlet 11 Servlet Filters 16 Event Listeners 17 Asynchronous Support 20 Nonblocking I/O 22 Web Fragments 24 Security 25 Resource Packaging 28 Error Mapping 29 Handling Multipart Requests 30 Upgrade Processing 31 3. JavaServer Faces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Facelets 34 Resource Handling 37 Composite Components 38 Request Processing Life-Cycle Phases 41 Ajax 43 HTTP GET 46 Server and Client Extension Points 47 v www.it-ebooks.info Validating Data 50 Navigation Rules 51 Faces Flow 51 Resource Library Contracts 57 Passthrough Attributes and HTML5-Friendly Markup 59 Component Tags 60 4. RESTful Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Resources 73 Binding HTTP Methods 77 Multiple Resource Representations 79 Binding a Request to a Resource 81 Entity Providers 82 Client API 84 Mapping Exceptions 87 Filters and Entity Interceptors 88 Validation of Resources 94 5. SOAP-Based Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Web Service Endpoints 98 Provider-Based Dynamic Endpoints 101 Endpoint-Based Endpoints 102 Web Service Client 103 Dispatch-Based Dynamic Client 105 Handlers 107 6. JSON Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Streaming API 112 Consuming JSON Using the Streaming API 112 Producing JSON Using the Streaming API 114 Object Model API 116 Consuming JSON Using the Object Model API 116 Producing JSON Using the Object Model API 117 7. WebSocket. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Annotated Server Endpoint 122 Programmatic Server Endpoint 128 Annotated Client Endpoint 132 Programmatic Client Endpoint 135 JavaScript WebSocket Client 137 Encoders and Decoders 138 vi | Table of Contents www.it-ebooks.info Integration with Java EE Security 142 8. Enterprise JavaBeans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Stateful Session Beans 145 Stateless Session Beans 148 Singleton Session Beans 150 Life-Cycle Event Callbacks 151 Message-Driven Beans 154 Portable Global JNDI Names 156 Transactions 157 Asynchronous Invocation 159 Timers 160 Embeddable API 164 EJB Lite 165 9. Contexts and Dependency Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Discovery of Beans 167 Injection Points 170 Qualifier and Alternative 171 Producer and Disposer 173 Interceptors 174 Decorators 178 Scopes and Contexts 179 Stereotypes 181 Events 182 Portable Extensions 183 Built-in Beans 185 Life-Cycle Callbacks 186 10. Concurrency Utilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Asynchronous Tasks 189 Schedule Tasks 194 Managed Threads 197 Dynamic Contextual Objects 198 11. Bean Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Built-in Constraints 203 Defining a Custom Constraint 207 Validation Groups 210 Method and Constructor Constraint 212 12. Java Transaction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Table of Contents | vii www.it-ebooks.info User-Managed Transactions 215 Container-Managed Transactions 216 @TransactionScoped 218 13. Java Persistence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Entities 219 Persistence Unit, Persistence Context, and Entity Manager 222 Schema Generation 226 Create, Read, Update, and Delete Entities 229 Entity Listeners 232 Stored Procedures 235 Validating the Entities 237 Transactions and Locking 239 Caching 241 14. Java Message Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Sending a Message 247 Receiving a Message Synchronously 251 Receiving a Message Asynchronously 253 Quality of Service 254 Temporary Destinations 255 15. Batch Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Chunk-Oriented Processing 258 Custom Checkpointing 263 Exception Handling 264 Batchlet Processing 265 Listeners 266 Job Sequence 267 Flow 268 Split 269 Decision 269 Partitioning the Job 271 16. Build an End-to-End Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Introduction 275 Software Requirements 275 Problem Statement 276 Lab Flow 277 Walkthrough of a Sample Application 278 Show Booking (JavaServer Faces) 283 Chat Room (Java API for WebSocket) 292 viii | Table of Contents www.it-ebooks.info
Description: