Skip Headers

Oracle9i Application Server Application Developer's Guide
Release 2 (9.0.2)

Part Number A95101-01
Go To Documentation Library
Go To Product List
Solution Area
Go To Table Of Contents
Go To Index

Go to previous page Go to next page

Creating Presentation Pages

You can create the presentation pages, which can contain data from business logic plus presentation elements, using different methods:

Contents of this chapter:

5.1 HTML Files

This option is valid for static pages only. If your pages have dynamic data, you have to generate the pages programmatically.

5.2 Servlets

Servlets enable you to generate pages programmatically. Using servlets, you can call business logic objects to obtain data, then assemble the page by adding in presentation elements. You can then send the completed page to the client.

Servlets can call methods in themselves and methods in other objects. Servlets can retrieve or update data in databases using JDBC or SQLJ.

Disadvantages of using servlets:

Servlets are a good choice for implementing state machines or controllers. State machines or controllers receive requests, make decisions based on parameters in the requests, and redirect the requests to the appropriate JSP for assembling the final display page to return to the clients. In the sample application, the controller is a servlet; see Section 4.3, "Controller".

See Oracle9iAS Servlet Developer's Guide for details on servlets.

5.2.1 Automatic Compilation of Servlets

One advantage to updating servlets is that Oracle9iAS has an auto-compile feature for servlets. You can place the uncompiled .java files for the servlets in the $J2EE_HOME/default-web-apps/WEB-INF/classes directory, and Oracle9iAS will compile the files for you. To enable the auto-compile feature, set the development attribute of the orion-web-app tag to "true". This tag is found in $J2EE_HOME/home/config/global-web-application.xml.


5.2.2 Example

For example, the following doGet() method in a servlet sends HTML data to the client:

public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    // Set the content type of the response

    // Get a print writer stream to write output to the response
    PrintWriter out = res.getWriter();

    // Send HTML to the output stream
    out.println("<TITLE>Employee Benefit Application</TITLE></HEAD>");
    out.println("<p>... more data here ...");
    // Close the HTML tags

5.2.3 Example: Calling an EJB

Here is an example of a servlet that calls an EJB object. Note how the servlet simply invokes methods on the EJB instance to get data. In this case, the servlet calls getName() and getPrice() methods on the EJB instance and embeds the return values within the presentation code.

import java.util.*;
import java.rmi.RemoteException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

public class ProductServlet extends HttpServlet {
   ProductHome home;

   public void init() throws ServletException {
      try {
         Context context = new InitialContext();
         home = (ProductHome)PortableRemoteObject.
                         narrow(context.lookup("MyProduct"), ProductHome.class);
      catch(NamingException e) {
         throw new ServletException("Error looking up home", e);

   public void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException {
      ServletOutputStream out = response.getOutputStream();
      try {
         Collection products = home.findAll();

         out.println("<head><title>My products</title></head>");
         out.println("<table border=\"2\">");

         Iterator iterator = products.iterator();
         while (iterator.hasNext())
            Product product = (Product)PortableRemoteObject.
                                     narrow(, Product.class);
            out.println("<tr><td>" + product.getName() + "</td><td>" +
                         product.getPrice() + "</td></tr>");
      catch(RemoteException e) {
         out.println("Error communicating with EJB-server: " + e.getMessage());
      catch(FinderException e) {
         out.println("Error finding products: " + e.getMessage());
      finally {
      }     // finally
   }     // doGet method

5.3 JSPs

Like servlets, JSP files enable you to combine HTML tags with Java commands. You do not have the println statements in JSP files like you do in servlets. Instead, you write your HTML tags as usual, but you add in special tags for JSP commands.

JSPs can do everything that servlets can do. For example, JSPs can invoke other classes and connect to the database to retrieve data or update data in the database.

See Oracle9iAS Support for JavaServer Pages Reference for details on JSPs.

5.3.1 Tag Libraries

In addition, JSPs enable you to define custom tags in tag libraries. Tag libraries enable you to define the behavior of your custom tags. Your JSPs can then access the tag libraries and use the custom tags. This enables you to standardize presentation and behavior across all your JSP files.

Here are few examples of how you can use custom tags and tag libraries. You can use them to:

See Oracle9iAS JSP Tag Libraries and Utilities Reference for details on tag libraries.

5.3.2 Minimal Coding in JSPs

Although you can use as much Java in your JSPs as you like, the file can be difficult to read and debug if it is interleaved with JSP scriptlets and HTML. You will get a cleaner design for your application if you place all the business logic code outside the JSP files. The JSP scriptlets in your files can call out to Enterprise JavaBeans and other Java classes to run business logic. These objects then return the data or status to your JSP file, where you can extract the data and display the data using HTML or XML.

Another benefit of excluding business logic code from your JSPs is that you can have web page designers who might not be familiar with Java work on the JSP page. They can design the look of the page, using placeholders for the real data. Your developers, who might not want to bother with HTML, can be working on the business logic in other files simultaneously.

5.3.3 Multiple Client Types

If you are supporting different client types (browsers and wireless clients), you can have two versions of JSP files: one that returns HTML and one that returns XML. One important note is that both files make the same calls to the same objects to perform business logic. This is what the sample application does.

In the Employee Benefit application, all the presentation code, even the pages for error conditions, are written in JSP files, and the JSP files do not contain any business logic code. The application uses one file for browsers (for example, addBenefitToEmployee.jsp) and a similar file for wireless clients (for example, addBenefitToEmployeeWireless.jsp). The wireless version of the file contains XML instead of HTML.

Go to previous page Go to next page
Copyright © 2002 Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Go To Product List
Solution Area
Go To Table Of Contents
Go To Index