2. Using the Tutorial Examples 3. Getting Started with Web Applications Troubleshooting Duke's Bookstore Database Problems Handling Servlet Life-Cycle Events Specifying Event Listener Classes Controlling Concurrent Access to Shared Resources Getting Information from Requests Filtering Requests and Responses Programming Customized Requests and Responses 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 | Invoking Other Web ResourcesWeb components can invoke other web resources in two ways: indirectly and directly.A web component indirectly invokes another web resource when it embeds a URLthat points to another web component in content returned to a client. Inthe Duke’s Bookstore application, most web components contain embedded URLs that point toother web components. For example,ShowCartServlet indirectly invokes theCatalogServlet through the followingembedded URL: /bookstore1/catalog A web component can also directly invoke another resource while it is executing.There are two possibilities: The web component can include the content of anotherresource, or it can forward a request to another resource. To invoke a resource available on the server that is running a webcomponent, you must first obtain aRequestDispatcher object using thegetRequestDispatcher("URL") method. You can get aRequestDispatcher object from either a request or the webcontext; however, the two methods have slightly different behavior. The method takes thepath to the requested resource as an argument. A request can take arelative path (that is, one that does not begin with a/), butthe web context requires an absolute path. If the resource is not availableor if the server has not implemented aRequestDispatcher object for thattype of resource,getRequestDispatcher will return null. Your servlet should be prepared todeal with this condition. Including Other Resources in the ResponseIt is often useful to include another web resource (for example, banner contentor copyright information) in the response returned from a web component. To includeanother resource, invoke theinclude method of aRequestDispatcher object: include(request, response); If the resource is static, theinclude method enables programmatic server-side includes. Ifthe resource is a web component, the effect of the method is tosend the request to the included web component, execute the web component, andthen include the result of the execution in the response from the containingservlet. An included web component has access to the request object, but itis limited in what it can do with the response object:
The banner for the Duke’s Bookstore application is generated byBannerServlet. Notethat bothdoGet anddoPost are implemented becauseBannerServlet can be dispatched fromeither method in a calling servlet. public class BannerServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { output(request, response); } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { output(request, response);}private void output(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<body bgcolor=\"#ffffff\">" + "<center>" + "<hr> <br> " + "<h1>" + "<font size=\"+3\" color=\"#CC0066\">Duke’s </font>" + <img src=\"" + request.getContextPath() + "/duke.books.gif\">" + "<font size=\"+3\" color=\"black\">Bookstore</font>" + "</h1>" + "</center>" + "<br> <hr> <br> "); }}Each servlet in the Duke’s Bookstore application includes the result fromBannerServlet usingthe following code: RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/banner");if (dispatcher != null) dispatcher.include(request, response);}Transferring Control to Another Web ComponentIn some applications, you might want to have one web component dopreliminary processing of a request and have another component generate the response. For example,you might want to partially process a request and then transfer to anothercomponent depending on the nature of the request. To transfer control to another web component, you invoke theforward method ofaRequestDispatcher. When a request is forwarded, the request URL is set tothe path of the forwarded page. The original URI and its constituent partsare saved as request attributesjavax.servlet.forward.[request-uri|context-path|servlet-path|path-info|query-string]. Thetut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/dispatcher/Dispatcher servlet, used by aversion of the Duke’s Bookstore application described inThe Example JSP Pages, saves the path information fromthe original URL, retrieves aRequestDispatcher from the request, and then forwardsto the JSP page,tut-install/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp. public class Dispatcher extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { RequestDispatcher dispatcher = request. getRequestDispatcher("/template.jsp"); if (dispatcher != null) dispatcher.forward(request, response); } public void doPost(HttpServletRequest request, ...}Theforward method should be used to give another resource responsibility for replyingto the user. If you have already accessed aServletOutputStream orPrintWriter object withinthe servlet, you cannot use this method; doing so throws anIllegalStateException. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |