Application Techniques

Using a Servlet to Retrieve Data

How to use a servlet to manage a directory of HTML pages stored in a database.

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

The example calls a servlet from an HTML edit control on a form. Whenever there is a request for an URL from the directory being managed, this servlet locates the correct row in the database and returns the HTML page to the requestor.

The form's "Show Page" button uses the showPage() method to gets the URL entered in the url text field and sends the HTTP request to the SilverStream Server. This line of code is from the actionPerformed event on btnShowPage:

  agBrowser.showPage(agGeneral.getDatabaseURL() + fldURL.getText()); 

The server produces a servlet trigger in response to the HTTP request to an URL. The listening business object that has a servlet trigger for this URL is instantiated, then executes the servlet's service() method.

Creating a business object with a servlet trigger   Top of page

To create a business object with a servlet trigger, do the following:

  1. From SilverStream Designer, select one of the databases, such as Examples3. Click Objects, the New icon, followed by New Object.

  2. Select servlet as the business object's trigger.

  3. Set its URL to Articles.

    NOTE   This specifies that the URL is the http://localhost/Examples3/Articles table in the database. This also specifies that if this business object gets an URL that begins with Articles, it invokes the servlet.

  4. Name the business object bosrvletArticles.

Adding a dataset to the business object   Top of page

Add a dataset to the business object by clicking the dataset icon in the Business Object Designer. Then set the data properties as follows:

This allows the servlet (the triggered business object) to access columns in the articles table.

Coding the servlet to query and return the result   Top of page

The following code represents the service() method in bosrvletArticles. This method gets the data set that is associated with the business object. It creates a query for records from the requested URL and sends the query. From the records that it retrieves (typically one record), it displays the contents of the articleData column.

  public void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res) throws javax.servlet.ServletException, 
    AgoHttpRequestEvent request = (AgoHttpRequestEvent)req; 
    HttpServletResponse response = (HttpServletResponse)res; 
        String url = request.getPathInfo(); 
        if (!url.equals("")) 
           url = url.substring(1); 
          // Get the AgaData object specified by "dataArticles". 
          // This is the data set associated with the  
          // servlet business object. 
          AgaData dataArticles = request.getAgaData("dataArticles"); 
         // Create a query for data from the articles table  
           // for records whose url column has the same URL value as 
          // the request. 
         String sQuery = "articles.url='articles/" + url + "'"; 
         // Send the query by calling dataArticles.query(). 
          // The query result is stored in dataArticles. 
          byte[] articleData; 
          String sArticleData = ""; 
          // To display the result, go to the first record of  
          // the retrieved data. 
           if (dataArticles.gotoFirst()) 
              // Get the value of the "articledata" property and 
              // convert it to a String. "articledata" is the 
              // property name you have given the 
              // articles.articledata column. 
              sArticleData =(String)dataArticles. 
              sArticleData = "" + 
                 "No page was found for URL:  " + url + 
            // Get the String as bytes and write the servlet 
            // response using the output stream.  
            OutputStream out = response.getOutputStream(); 
            // Set the MIME type for the response. 
     catch (Exception e) 
        // Call sendError on the ReplyEvent 

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