How to call a helper utility object from a servlet to build an HTML page.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
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.

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, java.io.IOException
{
AgoHttpRequestEvent hRequest = (AgoHttpRequestEvent) req;
AgiHttpServletResponse hResponse = (AgiHttpServletResponse) res;
// instantiate the helper object
boHTMLHelper htmlHelper = new boHTMLHelper();
String sHTML = htmlHelper.startPage("Departments");
sHTML += htmlHelper.startBody(
"BACKGROUND=\"../SilverStream/Objectstore/
Images/ExamplesBackground.JPG\"");
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 =
((AgoHttpRequestEvent)req).getAgaData(
"dataDepartments");
try
{
// Initiate the query
dataDepartments.query(null);
// 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 java.io.IOException(e.toString());
}
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
hResponse.setStatus(hResponse.SC_OK);
// Output the HTML
OutputStream out = hResponse.getOutputStream();
out.write(sHTML.getBytes());
return;

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+"
</CAPTION></BOLD><TR>";
//column headers
for (int i=0;i<numberOfColumns;i++)
{
sHTML=sHTML+"<TH>"+myAgaData.getPropertyName(i)+"</TH>";
}
sHTML=sHTML+"</TR>";
//column data
try
{
while (myAgaData.gotoNext())
{
sHTML=sHTML+"<TR>";
for (int i=0;i<numberOfColumns;i++)
{
sHTML=sHTML+"<TD>"+myAgaData.getProperty(i)+"</TD>";
}
sHTML=sHTML+"</TR>";
}
}
catch (Exception e)
{
sHTML="<STRONG>" + e + "</STRONG>";
}
sHTML=sHTML+"</TABLE>";
return sHTML;
}