How to use a servlet to manage a directory of HTML pages stored in a database.
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 |
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.
To create a business object with a servlet trigger, do the following:
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.
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.
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, java.io.IOException
{
AgoHttpRequestEvent request = (AgoHttpRequestEvent)req;
HttpServletResponse response = (HttpServletResponse)res;
try
{
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.
dataArticles.query(sQuery);
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.
getProperty("articledata");
}
else
{
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();
out.write(sArticleData.getBytes());
// Set the MIME type for the response.
response.setContentType("text/html");
response.setStatus(response.SC_OK);
}
return;
}
catch (Exception e)
{
// Call sendError on the ReplyEvent
response.sendError
(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
e.toString());
return;
}
}