• No results found

Getting references to the built-in objects

In document HAHTsite IDE Programming Guide (Page 112-117)

Depending on whether you are developing a Java project or a HAHTtalk Basic project, you will use different methods to get references to the Server Object Model’s built-in objects.

GeneratedSession, respectively. You can add variables and methods to these classes and reference them from each dynamic page.

Application and Session references in a dynamic page

In a Java project, the generated code for a dynamic page defines two variables, hahtApplication and hahtSession, to refer to the Application and Session objects:

Both hahtApplication and hahtSession are set in the constructor for the page, so their values are accessible anywhere in the code for that page. For example, if you have declared a variable lastName in HahtSession.java, this is how you would refer to it on a dynamic page:

hahtSession.lastName

Application and Session references in other Java source files

In a Java source file other than a dynamic page, there are two ways to get a reference to the Application or Session object. If you simply need to use a method defined for the Application or Session class, you can use this procedure:

1 Get a reference to the current application or session, using Haht.getApplication() or Haht.getSession().

2 Use that reference to access the variable or method. For example, you can get a URL string, given a project ID:

However, you may also need to access variables and methods that you have added to HahtApplication.java or HahtSession.java. In that case, you must use the second procedure, which is actually a superset of the first. (This

Java

protected HahtApplication hahtApplication; protected HahtSession hahtSession;

...

hahtApplication = (HahtApplication) Haht.getApplication(); hahtSession = (HahtSession) Haht.getSession();

Java

String myURL = Haht.getSession().getURL ("89392C87BB91D2119141A02550C10000");

is the procedure used to define hahtSession and hahtApplication on dynamic pages.)

1 Get a reference to the current application or session, using Haht.getApplication() or Haht.getSession().

2 Cast the return value to type HahtApplication or HahtSession.

3 Use that reference to access the variable or method. For example, suppose that in the code for a dynamic page, you want to print the value of a variable you’ve stored in the Application object. Your code would look something like this:

Session references in master projects and referred projects

Both master projects and referred projects contain GeneratedSession.java and HahtSession.java files; however, in a master project, the class

GeneratedSession extends com.haht.project.Session, while in a referred project, the GeneratedSession class extends the class

com.haht.project.UtilitySession (see the figure below). The important point to note here is that the UtilitySession class does not represent a user session, as Session does; UtilitySession is a very general class from which GeneratedSession inherits only a small amount of functionality.

Java

HahtApplication myApp =

(HahtApplication)Haht.getApplication(); out.print(myApp.myVar);

In a master project:

• A Session object represents an entire session.

• A GeneratedSession object contains master-project extensions to the Session class such as references to objects that have been dragged and dropped onto the project’s Session folder. For example, the

GeneratedSession object might contain a reference to a data agent representing a session recordset created by dragging and dropping a table onto the Session folder.

• A HahtSession object contains user-defined variables and methods. A referred project’s GeneratedSession and HahtSession objects are analogous to those in a master project. However, a referred project has no Session object associated with it. There is only one Session object per user session.

How do you get references to these objects? HAHTsite generates the code that obtains references to these objects and assigns those references to variables. The matrix below shows the names you use to access objects in either a master project or a referred project from either a master project or a referred project.

com.haht.project.Session GeneratedSession HahtSession com.haht.project. UtilitySession GeneratedSession HahtSession

Briefly, here’s how you might use these names. Suppose that from your master project, you want to refer to a session recordset (a data agent) defined in the referred project ReferredProject. For the purposes of this example, assume that the session recordset was created by dragging a shared query named

SharedQuery to the project’s Session folder. You could refer to this data agent using the following code:

protected com.haht.project.datamanager.DataAgent da1 = ReferredProject_hahtSessionExt.getSharedQuery(); Request and Response objects

The run method for each page takes two arguments: aRequest and aResponse, which refer to the Request and Response objects, respectively. You can use

Name Use

hahtSession In a master project, you should use this name to refer to the session’s Session object. You can also use it in a master project to refer to the master project’s GeneratedSession and HahtSession objects, but you shouldn’t do this if your project is, or might later become, a referred project. HAHTsite provides another name to use in this situation.

In a referred project, you can only use the name to refer to the session’s Session object.

projectName_hahtSessionExt In a master project, you can use a name of this form to refer to the master project’s extended objects (its GeneratedSession and HahtSession objects) and to a referred project’s extended objects. (The

projectName portion of the name will vary depending on the project.)

In a referred project, you use a name of this form to refer to the referred project’s extended objects.

Page object

The Session object’s returnHAHTPageObject method instantiates a page object and returns a reference to it; if the page has already been instantiated, it simply returns the object reference. For details, see “Calling a dynamic page from Java” on page 30.

In document HAHTsite IDE Programming Guide (Page 112-117)