The Java applet language environment lets you easily generate HTML tags for Java applets in your Net.Data applications. When you call the Java applet language environment, you specify the name of your applet and pass any parameters that the applet needs. The language environment processes the macro and generates the HTML applet tags, which the Web browser uses to run the applet.
Additionally, Net.Data provides a set of interfaces your applet can use to access table parameters. These interfaces are contained in the class, DTW_Applet.class.
The following sections describe how to use the Java applet language environment to run your Java applets.
Verify that the following configuration statement is in the initialization file, on one line:
ENVIRONMENT (DTW_APPLET) DTWJAVA ( OUT RETURN_CODE )
See Environment Configuration Statements to learn more about the Net.Data initialization file and language environment ENVIRONMENT statements.
Before using the Net.Data Java applet language environment, you need to determine which applets you plan to use or which applets you need to write. See your Java documentation for more information on creating applets.
You specify a call to the applet language environment with a Net.Data function call. No declaration is needed for the function call. The syntax for the function call is shown here:
@DTWA_AppletName(parm1, parm2, ..., parmN)
To write a macro that generates applet tags:
%define{ DATABASE = "celdial" <=Name of the database MyGraph.codebase = "/netdata-java/" <=Required applet attribute MyGraph.height = "200" <=Required applet attribute MyGraph.width = "400" <=Required applet attribute MyTitle = "Celdial results" <=Name of the Web page MyTable = %TABLE(all) <=Table to store query results %}
%FUNCTION(DTW_SQL) mySQL(OUT table){ select name, ages from ibmuser.guests %}
For example:
%HTML(report){ @mySQL(MyTable) <=A call to mySQL @DTWA_MyGraph(MyTitle, DTW_COLUMN(ages) MyTable) <=Applet function call %}
You can specify attributes for applet tags anywhere in your Net.Data macro. Net.Data substitutes all variables that have the form AppletName.attribute into the applet tag as attributes. The syntax for defining an attribute on an applet tag is shown here:
%define AppletName.attribute = "value"
The following attributes are required for all applets:
The following attributes are optional:
For example, if your applet is called MyGraph, you can define these required attributes as shown here:
%DEFINE{ MyGraph.codebase = "/netdata-java/" MyGraph.height = "200" MyGraph.width = "400" %}
The actual assignment need not be in a DEFINE section. You can set the value with the DTW_ASSIGN function. If you do not define a variable for AppletName.code variable, Net.Data adds a default code parameter to the applet tag. The value of the codeparameter is AppletName.class, where AppletName is the name of your applet.
You define a list of parameters to pass to the Java applet language environment in the function call. You can pass parameters that include:
When you pass a parameter, Net.Data creates a Java applet PARAM tag in the HTML output with the name and value that you assign to the parameter. You cannot pass string literals or results of function calls.
You can use Net.Data variables as parameters. If you define a variable in the DEFINE block of the macro and pass the variable value in the DTWA_AppletName function call, Net.Data generates a PARAM tag that has the same name and value as the variable. For example, given the following macro statement:
%define{ ... MyTitle = "This is my Title" %} %HTML(report){ @DTWA_MyGraph( MyTitle, ...) %}
Net.Data produces the following applet PARAM tag:
<param name = 'MyTitle' value = "This is my Title" />
Net.Data automatically generates a PARAM tag with the name DTW_NUMBER_OF_TABLES every time the Java applet language environment is called, specifying whether the function call has passed any table variables. The value is the number of table variables that Net.Data uses in the function. If no table variables are specified in the function call, the following tag is generated:
<param name = "DTW_NUMBER_OF_TABLES" value = "0" />
You can pass one or more Net.Data table variables as parameters on the function call. If you specify a Net.Data table variable on a DTWA_AppletName function call, Net.Data generates the following PARAM tags:
This tag specifies the names of the tables to pass. The tag has the following syntax:
<param name = 'DTW_TABLE_i_NAME' value = "tname" />
Where i is the number of the table based on the ordering of the function call, and tname is the name of the table.
PARAM tags are generated to specify the number of rows and columns a particular table. This tag has the following syntax:
<param name = 'DTW_tname_NUMBER_OF_ROWS' value = "rows" /> <param name = 'DTW_tname_NUMBER_OF_COLUMNS' value = "cols" />
Where the name of the table is tname, rows is the number of rows in the table, and cols is the number of columns in the table. This pair of tags is generated for each unique table specified in the function call.
This PARAM tag specifies the column name of a particular column. This tag has the following syntax:
<param name = 'DTW_tname_COLUMN_NAME_j' value = "cname" />
Where the table name is tname, j is the column number, and cname is the name of the column in the table.
This PARAM tag specifies the values at a particular row and column. This tag has the following syntax:
<param name = 'DTW_tname_cname_VALUE_k' value = "val" />
Where the table name is tname, cname is the column name, k is the row number, and val is the value that matches the value in the corresponding row and column.
You can pass a table column as a parameter on a function call to generate tags for a specific column. Net.Data generates the corresponding applet tags only for the specified column. A table column parameter uses the following syntax:
@DTWA_AppletName(DTW_COLUMN( x )Table)
Where x is the name or number of the column in the table.
Table column parameters use the same applet tags defined for the table parameters.
The variable DTW_APPLET_ALTTEXT specifies the text to display on browsers that do no support Java or have turned Java support off. For example, the following variable definition:
%define DTW_APPLET_ALTTEXT = "<p>Sorry, your browser is not Java-enabled."</p>
produces the following HTML tag and text:
<p>Sorry, your browser is not Java-enabled.</p><
If this variable is not defined, no alternate text is displayed.
The following example demonstrates a Net.Data macro that calls the Java applet language environment and the resulting applet tag that the language environment generates.
The Net.Data macro contains the following function calls to the Java applet language environment:
%define{ DATABASE = "celdial" DTW_APPLET_ALTTEXT = "<p>Sorry, your browser is not Java-enabled."</p> DTW_DEFAULT_REPORT = "no" MyGraph.codebase = "/netdata-java/" MyGraph.height = "200" MyGraph.width = "400" MyTitle = "This is my Title" %} %FUNCTION(DTW_SQL) mySQL(OUT table){ select name, ages from ibmuser.guests %} %HTML(report){ @mySQL(MyTable) @DTWA_MyGraph(MyTitle, DTW_COLUMN(ages) MyTable) %}
The Net.Data macro lines in the DEFINE section specify the attributes of the applet tag:
MyGraph.codebase = "/netdata-java/" MyGraph.height = "200" MyGraph.width = "400"
The language environment generates an applet tag with the following qualifiers:
<applet code='MyGraph.class' codebase='/netdata-java/' width='400' height='200' >
Net.Data returns the SQL query results from the SQL section of the Net.Data macro in the output table, MyTable. This table is specified in the DEFINE section:
MyTable = %TABLE(all)
The call to the applet in the macro is specified in the HTML section:
@DTWA_MyGraph(MyTitle, DTW_COLUMN(ages) MyTable)
Based on the parameters in the function call, Net.Data generates the complete applet tag containing the information about the result table, such as the number of columns, the number of rows returned, and the result rows. Net.Data generates one parameter tag for each cell in the result table, as shown in the following example:
<param name = 'DTW_MyTable_ages_VALUE_1' value = "35" />
The parameter name, DTW_MyTable_ages_VALUE_1, specifies the table cell (row 1, column ages) in the table, MyTable, which has a value of 4. The keyword, DTW_COLUMN, in the function call to the applet, specifies that you are interested only in the column ages of the resulting table, MyTable, shown here:
@DTWA_MyGraph( MyTitle, DTW_COLUMN(ages) MyTable )
The following output shows the complete applet tag that Net.Data generates for the example:
<applet code='MyGraph.class' codebase='/netdata-java/' width='400' height='200' > <param name = 'MyTitle' value = "This is my Title" /> <param name = 'DTW_NUMBER_OF_TABLES' value = "1" /> <param name = 'DTW_TABLE_1_NAME' value = "MyTable" /> <param name = 'DTW_MyTable_NUMBER_OF_ROWS' value = "5" /> <param name = 'DTW_MyTable_NUMBER_OF_COLUMNS' value = "1" /> <param name = 'DTW_MyTable_COLUMN_NAME_1' value = "ages" /> <param name = 'DTW_MyTable_ages_VALUE_1' value = "35" /> <param name = 'DTW_MyTable_ages_VALUE_2' value = "32" /> <param name = 'DTW_MyTable_ages_VALUE_3' value = "31" /> <param name = 'DTW_MyTable_ages_VALUE_4' value = "28" /> <param name = 'DTW_MyTable_ages_VALUE_5' value = "40" /> <p>Sorry, your browser is not Java-enabled.</p> </applet>
Net.Data provides a set of interfaces in a class called DTW_Applet.class, which you can use with your Java applets to help process the PARAM tags that are generated for table variables. You can create an applet that extends this interface to call the routines from your applet.
Net.Data provides these interfaces:
To access the interfaces, use the EXTENDS keyword in your applet code to subclass your applet from the DTW_APPLET class, as shown in the following example:
import java.io.*; import java.applet.Applet; public class myDriver extends DTW_Applet { public void init() { super.init(); if (GetNumberOfTables() > 0) { String [] tables = GetTableNames(); printTables(tables); } } private void printTables(String[] tables) { String table_name; for (int i = 0; i < tables.length; i++) { table_name = tables[i]; printTable(table_name); } } private void printTable(String table_name) { int nrows = GetNumberOfRows(table_name); int ncols = GetNumberOfColumns(table_name); System.out.println("Table: " + table_name + " has " + ncols + " columns and " + nrows + " rows."); String [] col_names = GetColumnNames(table_name); System.out.println("----------------------------------------"); for (int i = 0; i < ncols; i++) System.out.print(" " + col_names[i] + " "); System.out.println("\n--------------------------------------"); String [][] mytable = GetTable(table_name); for (int j = 0; j < nrows; j++) { for (int i = 0; i < ncols; i++) System.out.print(" " + mytable[i][j] + " "); System.out.println("\n"); } } }