2. Using the Tutorial Examples 3. Getting Started with Web Applications Troubleshooting Duke's Bookstore Database Problems Controlling Concurrent Access to Shared Resources Getting Information from Requests Filtering Requests and Responses Programming Customized Requests and Responses Including Other Resources in the Response Transferring Control to Another Web Component Associating Objects with a Session Notifying Objects That Are Associated with a Session Notifying Methods to Shut Down Creating Polite Long-Running Methods Further Information about Java Servlet Technology 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 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 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 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 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Servlet Life CycleThe life cycle of a servlet is controlled by the container inwhich the servlet has been deployed. When a request is mapped to aservlet, the container performs the following steps.
If the container needs to remove the servlet, it finalizes the servlet bycalling the servlet’sdestroy method. Finalization is discussed inFinalizing a Servlet. Handling Servlet Life-Cycle EventsYou can monitor and react to events in a servlet’s life cycle bydefining listener objects whose methods get invoked when life-cycle events occur. To usethese listener objects you must define and specify the listener class. Defining the Listener ClassYou define a listener class as an implementation of a listener interface.Table 4-2lists the events that can be monitored and the corresponding interface that mustbe implemented. When a listener method is invoked, it is passed an eventthat contains information appropriate to the event. For example, the methods in theHttpSessionListener interface are passed anHttpSessionEvent, which contains anHttpSession. Table 4-2 Servlet Life-Cycle Events
Thetut-install/javaeetutorial5/examples/web/bookstore1/src/java/com/sun/bookstore1/listeners/ContextListener class creates and removes the database access and counter objects usedin the Duke’s Bookstore application. The methods retrieve the web context object fromServletContextEvent and then store (and remove) the objects as servlet context attributes. import database.BookDBAO;import javax.servlet.*;import util.Counter;import javax.ejb.*;import javax.persistence.*;public final class ContextListener implements ServletContextListener { private ServletContext context = null; @PersistenceUnit EntityManagerFactory emf; public void contextInitialized(ServletContextEvent event) { context = event.getServletContext(); try { BookDBAO bookDB = new BookDBAO(emf); context.setAttribute("bookDB", bookDB); } catch (Exception ex) { System.out.println( "Couldn’t create database: " + ex.getMessage()); } Counter counter = new Counter(); context.setAttribute("hitCounter", counter); counter = new Counter(); context.setAttribute("orderCounter", counter); } public void contextDestroyed(ServletContextEvent event) { context = event.getServletContext(); BookDBAO bookDB = context.getAttribute("bookDB"); bookDB.remove(); context.removeAttribute("bookDB"); context.removeAttribute("hitCounter"); context.removeAttribute("orderCounter"); }}Specifying Event Listener ClassesYou specify an event listener class using thelistener element of the deploymentdescriptor. ReviewThe Example Servlets for information on how to specify theContextListener listener class. You can specify an event listener using the deployment descriptor editor of NetBeansIDE by doing the following:
Handling Servlet ErrorsAny number of exceptions can occur when a servlet executes. When an exceptionoccurs, the web container generates a default page containing the message A Servlet Exception Has Occurred But you can also specify that the container should return a specific errorpage for a given exception. Review the deployment descriptor file included with theexample to learn how to map the exceptionsexception.BookNotFound,exception.BooksNotFound, andexception.OrderExceptionreturned by the Duke’s Bookstore application toerrorpage.html. SeeMapping Errors to Error Screens for instructions on how to specify error pages using NetBeans IDE. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |