Getting started with the Host Access Class Library (HACL) is easy because the object model is so simple. You will typically use five or six primary classes, which will provide most of the function for any HACL applet. These classes are all associated with a session to a particular host computer and can be obtained from an instance of ECLSession.
The first thing to do is define a session with a host using the ECLSession class. It is constructed with a java.util.Properties object populated with keyword and value pairs which make up the session configuration. A session has a variety of configuration parameters including type (3270, 5250, VT), host, port, ID, presentation space dimensions (for example, 24 rows by 80 columns), code page, and others specific to the session type. All parameters, except host, have default values for 3270 emulation sessions, including the type.
The sample below defines a session to the specified host or TN3270/5250 server, but does not initiate communications.
ECLSession s = null; Properties p = new Properties(); // This is the name of the host or TN server p.put(ECLSession.SESSION_HOST, new String("myHost")); // Set the SESSION_CODEBASE parameter when running from a Web page. // The use of 'this' assumes that the current class extends // java.applet.Applet // p.put(ECLSession.SESSION_APPLET, this); try { s = new ECLSession(p); } catch (ECLErr e) { System.out.println(e.GetMsgText()); }
After defining a session with the target host, you can gain access to the presentation space and interact with it. The presentation space is encapsulated in the ECLPS class, and an instance of it can be obtained using the GetPS() method on ECLSession. ECLPS provides methods that manipulate text, perform searches, send keystrokes to the host, and work with the cursor.
The sample below gets an instance of ECLPS from the session established above.
ECLPS ps = s.GetPS()
Once an instance of ECLPS is established, you can register to receive notification of presentation space changes. Registered objects are notified whenever the presentation space is changed. This event notification model is the primary mechanism used by an application to drive interactions with the presentation space.
The sample code below registers the current class with the instance of ECLPS and starts communications with the target host. Notice that the sample waits to start the session with the host (the call to StartCommunication()) until after it has registered with ECLPS. This is done to ensure that no presentation space events are missed which could cause incorrect behavior in the applet.
try { ps.RegisterPSEvent(this); } catch(ECLErr e) { System.out.println(e.GetMsgText()); } // Start the session after registration so we don't // miss any presentation space events. s.StartCommunication()
Now you are registered for presentation space events, but still need to implement methods which allow your class to receive the events. These methods are defined by the ECLPSListener interface, and the class you want to register must implement this interface.
The ECLPSListener interface is comprised of three methods which will handle different kinds of events occurring within the presentation space. The PSNotifyEvent() method handles normal, non-error events and is the main method for receiving and handling events. The PSNotifyStop() method handles stop events, and the PSNotifyError() method handles errors which occur during event generation.
The following sample defines a simple PSNotifyEvent() method which recognizes screens and takes action on those different screens.
public void PSNotifyEvent(ECLPSEvent evt) { if (ps.SearchText("some text on the login screen", ps.SEARCH_FORWARD)) { ps.SendKeys("myUserID"+ps.FWDTAB_STR+"myPW"+ps.ENTER_STR); } return; }
The previous sample recognizes a particular host screen, such as a login prompt, then sends a user ID and password to the host starting at the current cursor location. Note that the sample does not show the other two methods, PSNotifyStop() and PSNotifyError(), which must be implemented to conform to the ECLPSListener interface.
That's all you need to get started with your first HACL applet!
Host
On-Demand provides the ability to load and run user-defined applets. A
user applet can be launched from the 'Run Applet' dialog or as a 'Startup
Applet'. The 'Run Applet' dialog can be displayed from either the button
bar or the 'Assist' pull-down menu. The dialog prompts for the name of a
user-defined class (without the .class extension), constructs an instance
of the class using the default constructor, and gives the class access to
the current session. The 'Startup Applet' can be specified in the session
configuration window on the Advanced tab.
Personal
Communications provides only the 'Run Java Applet' feature which is
similar to the 'Run Applet' feature of Host On-Demand.
The methods called when an applet is loaded are defined in an interface called ECLAppletInterface. This interface must be implemented by the main class of an applet which will be run using this feature.
To enable an applet to run within an active emulator window, the first thing we need to implement is a default constructor. The default constructor has no parameters and is called when a class is dynamically loaded. The default constructor does not have to contain any code.
The following sample defines a default constructor for an applet class called MyClass.
public MyClass() { }
The only other thing we have to define is the init() method, which is required for the implementation of ECLAppletInterface. This is the only method (besides the constructor) which is called when the applet is loaded, so we must make sure that the init() method gets the applet off and running. To do this, we will register with ECLPS to begin receiving PS notification events.
Simply registering for PS events may not trigger the applet to start because it is probably waiting for the first PS event, and there may not be any pending events when the applet is started. To work around this problem, we could either attempt to recognize the current presentation space right away, or we could generate our own PS event and let our PSNotifyEvent() method handle it.
The following sample defines the init() method and shows one way of handling the initial presentation space state. The sample assumes that MyClass implements ECLPSListener, in addition to ECLAppletInterface.
public void init(ECLSession session) { ps = session.GetPS(); // Register with ECLPS to receive updates and new screens try { ps.RegisterPSEvent(this); } catch(Exception e) { System.out.println("Registration Exception"); } // Call our PSNotifyEvent() method to handle the initial state of the PS PSNotifyEvent(new ECLPSEvent(ps)); }
Don't forget that we must also define the PSNotifyEvent(), PSNotifyStop(), and PSNotifyError() methods as required by the ECLPSListener interface. In addition, MyClass must define that it implements ECLAppletInterface in its class declaration.
[ Top of Page | Previous Page| Next Page | Table of Contents]