Application Techniques

Using a Servlet to Build an HTML Page

How to call a helper utility object from a servlet to build an HTML page.

About this technique



Triggered Business Object Techniques> Servlets


You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on Using servlet business objects in the Programmer's Guide

This page is built by a servlet that uses a helper utility object that provides various generic methods to format HTML output. The utility object is named boHTMLHelper, as specified in the import statement:

  import com.examples.utilityobjects.boHTMLHelper; 

The servlet has an AgaData bound to the Departments Table in the current database, and has a constructor that initializes the data. When the servlet is invoked, it uses a utility object to display some text and format data from the table.

Using the HTML helper object in the servlet   Top of page

The example uses the servlet's service() method to instantiate the utility object, then calls methods on the utility to format the page. It gets the data from the Departments table and initiates a query. Then it uses the makeTable() method from the utility object to format and display the data.

  public void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res) throws javax.servlet.ServletException, 
    AgoHttpRequestEvent hRequest = (AgoHttpRequestEvent) req; 
    AgiHttpServletResponse hResponse = (AgiHttpServletResponse) res; 
        // instantiate the helper object 
        boHTMLHelper htmlHelper = new boHTMLHelper(); 
        String sHTML = htmlHelper.startPage("Departments"); 
        sHTML += htmlHelper.startBody(  
         sHTML += htmlHelper.startFont( 
           "SIZE=5 COLOR=\"#2C547C\" FACE=\"Arial\""); 
        sHTML += htmlHelper.startSpan("STYLE=\"font-size:24;\""); 
        sHTML += "ServletHelper/DepartmentList.html<P>"; 
        sHTML += htmlHelper.endSpan(); 
        sHTML += htmlHelper.endFont(); 
        sHTML += htmlHelper.startFont( 
           "SIZE=3 COLOR=\"#2C547C\" FACE=\"Arial\""); 
        sHTML += htmlHelper.startSpan("STYLE=\"font-size:14;\""); 
        sHTML = sHTML + "This page is built by a servlet  
           that uses a helper utility to build " + 
          "the HTML.<BR>The servlet that builds this page is named 
           bosrvletHTMLHelperPage.<BR>" + 
          "The utility servlet is named boHTMLHelper.<p>"; 
        // Get the AgaData 
        AgaData dataDepartments =  
       // Initiate the query 
       // Call method on the utilty object, passing the AgaData 
       // object and a String to format the data 
          sHTML = sHTML + htmlHelper.makeTable( 
             dataDepartments, "Departments");   
        catch (Exception e) 
          System.out.println(this + ".Exception: " + e.toString()); 
          throw new; 
        sHTML += htmlHelper.endSpan(); 
        sHTML += htmlHelper.endFont(); 
        sHTML += htmlHelper.endBody(); 
        sHTML += htmlHelper.endPage(); 
        // Set the content type 
        hResponse.setContentType("text/html; charset=iso-8859-1");     
        // Update the header status field 
        // Output the HTML 
        OutputStream out = hResponse.getOutputStream(); 

Creating helper methods for HTML formatting   Top of page

The helper object boHTMLHelper contains the methods that format the data. The following shows the code for the makeTable() method, which formats the data from the AgaData passed to it from the servlet.

  public String makeTable(com.sssw.srv.busobj.AgaData myAgaData, String caption) throws java.sql.SQLException 
      int numberOfColumns = myAgaData.getPropertyCount(); 
      // start the table definition 
      String sHTML = "<TABLE BORDER><BOLD><CAPTION>"+caption+" 
      //column headers 
      for (int i=0;i<numberOfColumns;i++)  
      //column data 
        while (myAgaData.gotoNext())  
          for (int i=0;i<numberOfColumns;i++)  
      catch (Exception e)  
        sHTML="<STRONG>" + e + "</STRONG>"; 
      return sHTML; 

Copyright © 2000, SilverStream Software, Inc. All rights reserved.