Inside WebObjects
Developing Applications Using JavaServer Pages and Servlets
January 2002
Apple Computer, Inc. Enterprise Objects and Enterprise © 2002 Apple Computer, Inc. Objects Framework are trademarks of All rights reserved. NeXT Software, Inc., registered in the United States and other countries. No part of this publication may be reproduced, stored in a retrieval Java and all Java-based trademarks system, or transmitted, in any form or and logos are trademarks or by any means, mechanical, electronic, registered trademarks of Sun photocopying, recording, or Microsystems, Inc. in the United otherwise, without prior written States and other countries. permission of Apple Computer, Inc., Simultaneously published in the with the following exceptions: Any United States and Canada person is hereby authorized to store Even though Apple has reviewed this documentation on a single computer manual, APPLE MAKES NO for personal use only and to print WARRANTY OR REPRESENTATION, copies of documentation for personal EITHER EXPRESS OR IMPLIED, WITH use provided that the documentation RESPECT TO THIS MANUAL, ITS contains Apple’s copyright notice. QUALITY, ACCURACY, The Apple logo is a trademark of MERCHANTABILITY, OR FITNESS Apple Computer, Inc. FOR A PARTICULAR PURPOSE. AS A Use of the “keyboard” Apple logo RESULT, THIS MANUAL IS SOLD “AS (Option-Shift-K) for commercial IS,” AND YOU, THE PURCHASER, ARE purposes without the prior written ASSUMING THE ENTIRE RISK AS TO consent of Apple may constitute ITS QUALITY AND ACCURACY. trademark infringement and unfair IN NO EVENT WILL APPLE BE LIABLE competition in violation of federal FOR DIRECT, INDIRECT, SPECIAL, and state laws. INCIDENTAL, OR CONSEQUENTIAL No licenses, express or implied, are DAMAGES RESULTING FROM ANY granted with respect to any of the DEFECT OR INACCURACY IN THIS technology described in this book. MANUAL, even if advised of the Apple retains all intellectual property possibility of such damages. rights associated with the technology described in this book. This book is THE WARRANTY AND REMEDIES SET intended to assist application FORTH ABOVE ARE EXCLUSIVE AND developers to develop applications IN LIEU OF ALL OTHERS, ORAL OR only for Apple-labeled or WRITTEN, EXPRESS OR IMPLIED. No Apple-licensed computers Apple dealer, agent, or employee is authorized to make any modification, Every effort has been made to ensure extension, or addition to this warranty. that the information in this document is accurate. Apple is not responsible Some states do not allow the exclusion or for typographical errors. limitation of implied warranties or Apple Computer, Inc. liability for incidental or consequential 1 Infinite Loop damages, so the above limitation or Cupertino, CA 95014 exclusion may not apply to you. This 408-996-1010 warranty gives you specific legal rights, and you may also have other rights which Apple, the Apple logo, Mac, vary from state to state. Macintosh, and WebObjects are trademarks of Apple Computer, Inc., registered in the United States and other countries.
Contents
Figures, Listings, and Tables 5
Chapter 1 About This Book 7
Chapter 2 Deploying WebObjects Applications as Servlets 11 Servlets in WebObjects 12 Developing and Deploying a Servlet 13 Deploying an Existing Application as a Servlet 21 Cross-Platform Deployment 23 Configuring the Deployment Descriptor 23 Configuring the Servlet Container 24
Chapter 3 Developing JSP-Based WebObjects Applications 27 JSP-Page Writing Guidelines 28 Creating a JSP-Based Application 30 Passing Data From a JSP Page to a Component 34 Using WebObjects Classes in a JSP Page 38 Using Direct Actions in JSP Pages 42 Custom-Tag Reference 47
3 Apple Computer, Inc. January 2002
CONTENTS
Appendix A Special Issues 51 Deploying Multiple WebObjects WAR Files in a Single Servlet Container 51 Updating Servlet-Based WebObjects Applications to Future Versions of WebObjects 52
Glossary 55
Index 57
4 Apple Computer, Inc. January 2002
Figures, Listings, and Tables
Chapter 2 Deploying WebObjects Applications as Servlets 11 Figure 2-1 New WebObjects Application project with servlet-support 17 Figure 2-2 Build settings for a servlet project 18 Figure 2-3 Tomcat’s application-deployment directory 20 Table 2-1 Default host and port in Tomcat and WebLogic 21
Chapter 3 Developing JSP-Based WebObjects Applications 27 Figure 3-1 The Hello component in WebObjects Builder 31 Figure 3-2 Telling Project Builder where to put the WAR file 32 Figure 3-3 The output of Welcome.jsp 33 Figure 3-4 The DiningWell component in WebObjects Builder 35 Figure 3-5 The output of DiningWell.jsp 37 Figure 3-6 The MusicGenres component in WebObjects Builder 40 Figure 3-7 Telling Project Builder to copy WebObjects classes to the WAR file 41 Figure 3-8 The output of InternetRadio.jsp 42 Figure 3-9 The FoodInquiry component in WebObjects Builder 44 Figure 3-10 The output of LogIn.jsp 46 Listing 3-1 FavoriteFood.java 36 Listing 3-2 InternetRadio.jsp file 38 Listing 3-3 FoodInquiry.java 43 Table 3-1 Custom tags defined in WOTaglib_1_0.tld 28 Table 3-2 Attributes of the
5 Apple Computer, Inc. January 2002
FIGURES AND TABLES
6 Apple Computer, Inc. January 2002
CHAPTER 1
1 About This Book
JavaServer Pages (JSP) and servlets are important parts of Sun’s J2EE (Java 2 Platform, Enterprise Edition) architecture. JSP is a specification that defines interfaces that servlet-container vendors can implement to provide developers the ability to create dynamic Web pages, which are files with the extension .jsp. Servlet containers interpret these files and create servlets (also know as workhorse servlets) to process HTTP requests and produce responses. Servlets are server plug-ins that extend the capabilities of your HTTP server. They provide a straightforward deployment mechanism for your applications. Servlets are deployed inside servlet containers, which are plug-ins to your HTTP server.
You should read this book if you want to deploy your WebObjects applications inside a servlet container or want to take advantage of WebObjects components (both standard and custom) in your JSP pages.
Deploying WebObjects applications as servlets allows you to take advantage of the features that your servlet container provides. Keep in mind that deployment tools such as Monitor and wotaskd do work with servlets. WebObjects uses version 2.2 of the Servlet API, and version 1.1 of the JSP specification.
The book addresses two major points, each contained in its own chapter: Chapter 2, “Deploying WebObjects Applications as Servlets” (page 11), explains how you develop WebObjects applications to be deployed as servlets and how to add servlet capability to existing applications. Chapter 3, “Developing JSP-Based WebObjects Applications” (page 27), tells you how to write JSP-based applications, which can be thought of as JSP applications that use WebObjects technology or hybrids—applications that use JSP pages to accomplish some tasks and WebObjects components or direct actions to perform others.
7 Apple Computer, Inc. January 2002
CHAPTER 1
About This Book