Document Information Preface Part I Introduction 1. Overview 2. Using the Tutorial Examples Part II The Web Tier 3. Getting Started with Web Applications 4. Java Servlet Technology What Is a Servlet? Servlet Life Cycle Handling Servlet Life-Cycle Events Defining the Listener Class Specifying Event Listener Classes Handling Servlet Errors Sharing Information Using Scope Objects Controlling Concurrent Access to Shared Resources Accessing Databases Initializing a Servlet Writing Service Methods Getting Information from Requests Constructing Responses Filtering Requests and Responses Programming Filters Programming Customized Requests and Responses Specifying Filter Mappings Invoking Other Web Resources Including Other Resources in the Response Transferring Control to Another Web Component Accessing the Web Context Maintaining Client State Accessing a Session Associating Objects with a Session Notifying Objects That Are Associated with a Session Session Management Session Tracking Finalizing a Servlet Tracking Service Requests Notifying Methods to Shut Down Creating Polite Long-Running Methods Further Information about Java Servlet Technology 5. JavaServer Pages Technology 6. JavaServer Pages Documents 7. JavaServer Pages Standard Tag Library 8. Custom Tags in JSP Pages 9. Scripting in JSP Pages 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications Part III Web Services 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 18. Streaming API for XML 19. SOAP with Attachments API for Java Part IV Enterprise Beans 20. Enterprise Beans 21. Getting Started with Enterprise Beans 22. Session Bean Examples 23. A Message-Driven Bean Example Part V Persistence 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language Part VI Services 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 30. Securing Web Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 33. Transactions 34. Resource Connections 35. Connector Architecture Part VII Case Studies 36. The Coffee Break Application 37. The Duke's Bank Application Part VIII Appendixes A. Java Encoding Schemes B. About the Authors Index | | The Java EE 5 Tutorial |  |
The Example ServletsThis chapter uses the Duke’s Bookstore application to illustrate the tasks involved inprogramming servlets. The source code for the bookstore application is located in thetut-install/javaeetutorial5/examples/web/bookstore1/ directory, which is created when you unzip the tutorial bundle (seeBuilding the Examples). Table 4-1 lists the servlets that handle each bookstore function. You can find theseservlet classes intut-install/javaeetutorial5/examples/web/bookstore1/src/java/com/sun/bookstore1/. Each programming task is illustrated by one ormore servlets. For example,BookDetailsServlet illustrates how to handle HTTPGET requests,BookDetailsServlet andCatalogServlet show how to construct responses, andCatalogServlet illustrates how to tracksession information. Table 4-1 Duke’s Bookstore Example Servlets Function | Servlet |
|---|
Enter the bookstore | BookStoreServlet | Create the bookstore banner | BannerServlet | Browse the bookstore catalog | CatalogServlet | Put a bookin a shopping cart | CatalogServlet, BookDetailsServlet | Get detailed information on a specific book | BookDetailsServlet | Display the shoppingcart | ShowCartServlet | Remove one or more books from the shopping cart | ShowCartServlet | Buy the books inthe shopping cart | CashierServlet | Send an acknowledgment of the purchase | ReceiptServlet |
The data for the bookstore application is maintained in a database and accessedthrough the database access classdatabase.BookDBAO. Thedatabase package also contains the classBook which represents a book. The shopping cart and shopping cart items arerepresented by the classescart.ShoppingCart andcart.ShoppingCartItem, respectively. To deploy and run the application using NetBeans IDE, follow these steps: Perform all the operations described inAccessing Databases from Web Applications. In NetBeans IDE, select File→Open Project. In the Open Project dialog, navigate to: tut-install/javaeetutorial5/examples/web/ Select thebookstore1 folder. Select the Open as Main Project check box and the Open Required Projects check box. Click Open Project. In the Projects tab, right-click thebookstore1 project, and select Undeploy and Deploy. To run the application, open the bookstore URLhttp://localhost:8080/bookstore1/bookstore.
To deploy and run the application using Ant, follow these steps: In a terminal window, go totut-install/javaeetutorial5/examples/web/bookstore1/. Typeant. This command will spawn any necessary compilations, copy files to thetut-install/javaeetutorial5/examples/web/bookstore1/build/ directory, and create a WAR file and copy it to thetut-install/javaeetutorial5/examples/web/bookstore1/dist/ directory. Start the Application Server. Perform all the operations described inCreating a Data Source in the Application Server. To deploy the example, typeant deploy. The deploy target outputs a URL for running the application. Ignore this URL, and instead use the one shown in the next step. To run the application, open the bookstore URLhttp://localhost:8080/bookstore1/bookstore.
To learn how to configure the example, refer to the deployment descriptor (theweb.xml file), which includes the following configurations: Adisplay-name element that specifies the name that tools use to identify the application. A set offilter elements that identify servlet filters contained in the application. A set offilter-mapping elements that identify which servlets will have their requests or responses filtered by the filters identified by thefilter elements. Afilter-mapping element can define more than one servlet mapping and more than one URL pattern for a particular filter. A set ofservlet elements that identify all the servlet instances of the application. A set ofservlet-mapping elements that map the servlets to URL patterns. More than one URL pattern can be defined for a particular servlet. A set of error-page mappings that map exception types to an HTML page, so that the HTML page opens when an exception of that type is thrown by the application.
Troubleshooting Duke's Bookstore Database ProblemsThe Duke’s Bookstore database access object returns the following exceptions: BookNotFoundException: Returned if a book can’t be located in the bookstore database. This will occur if you haven’t loaded the bookstore database with data or the server has not been started or has crashed. You can populate the database by runningant create-tables. BooksNotFoundException: Returned if the bookstore data can’t be retrieved. This will occur if you haven’t loaded the bookstore database with data or if the database server hasn’t been started or it has crashed. UnavailableException: Returned if a servlet can’t retrieve the web context attribute representing the bookstore. This will occur if the database server hasn’t been started.
Because you have specified an error page, you will see the message The application is unavailable. Please try later. If you don’t specify an error page, the web container generates a defaultpage containing the message A Servlet Exception Has Occurred and a stack trace that can help you diagnose the cause ofthe exception. If you useerrorpage.html, you will have to look in the serverlog to determine the cause of the exception. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |