4. Advent SNMP Package Tutorial: An Applet Example

You can build JAVA applets as well using much the same methods as we did for applications. However, there are a couple of small differences to keep in mind.

Let's build a simple applet to receive and print SNMP traps. Our applet will have only a label and a text area for printing the traps we receive. This will illustrate the use of callbacks for received PDUs.

Applets need to implement the init(), start(), stop() and destroy() methods. As the names indicate, they're used by the browser (or appletviewer) to initialize, start, stop and destroy the applet that runs in the browser. The browser usually initializes an applet only once, when the class is loaded.

Start is run when the applet is visited, or shown on screen. Stop is called when applet is no longer on the screen. Destroy is called when applet is no longer needed and will not be re-visited without loading classes again.

We wish to use the callback facility which is ideal for our application where we just print traps that come in. For this we need to implement the SnmpClient interface in the SNMP Package.

So we begin with the class definition which extends applet (as all applets) and implements SnmpClient. We need a few variables in multiple methods, including the SnmpAPI and the TextArea, which we define.

We allow the user to specify two configurable parameters via the applet's HTML file. These are the port number and the MIB file to be loaded.