• No results found

What’s in a Java page?

In document HAHTsite IDE Programming Guide (Page 39-47)

For each dynamic page, the IDE generates a file (Hspagename.java) that defines a class Hspagename as a subclass of Page. The source code for each dynamic page has these contents:

• A package statement • Import statements

• Class declaration and constructor • A run method

• An exception handler • A form handler

• Places to add classes and methods

The following sections explain each of these topics, using as an example the Java source for a page that simply displays the current date.

A package statement

The package name is the name of the project.

For more information about package names, see “Naming conventions for Java projects” on page 30.

Import statements

Each page source file imports several packages, including the HAHTsite packages for the Server Object Model and for data management. You can add your own import statements using the HTML editor’s Server-side Code view.

Class declaration and constructor

Each page has a constructor, called once per page per session to create the page object. If the page contains form fields, they are initialized here. You can also add custom code to the constructor.

package ExamplProj; //////////////////////////////////////////////////////////////// // IMPORTS //////////////////////////////////////////////////////////////// import com.haht.*; import com.haht.project.*; import com.haht.io.*; import com.haht.project.form.*; import com.haht.project.datamanager.*; import com.haht.ado.Abstract.*; import java.io.*; import java.util.*; // custom imports import com.haht.access.*; import java.text.*;

The constructor also gets references to both the Application object and the Session object — references you can use anywhere in a dynamic page. These references are very useful in accessing application-level and session-level variables.

A run method

Each page has a run method, with two arguments: the Request object and the Response object. The run method emits any header lines — for example,

Content-type: text/html

Note that you can override the content-type header by another call to the Response object’s setContentType method. See “Adding content information to a header line” on page 127.

After emitting header lines, the run method then sets up the basic structure of the page, which would look like this in HTML:

<HTML>

<HEAD>...</HEAD> <BODY>...</BODY> </HTML>

public class HsShowDate extends com.haht.project.Page // Specify implements declarations here.

{ /**

* Default constructor for HsShowDate *

**/

public HsShowDate () {

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

...

// Custom constructor code.

When you add in-line code to a page, it’s normally added here, between the <BODY> and </BODY> tags. (Some code — such as setting a cookie — must be output with the HTTP headers. HAHTsite automatically handles this code correctly. See “Writing headers” on page 126.)

Notice the marked sections where you can add local variables, initialization code, and custom HTTP response headers (two places). You can also edit any in-line code. In this example, the page contains two lines of in-line code, to set the date.

/**

* Runs the page, producing HTML. *

* @param aRequest - HTTP request for this page * @param aResponse - HTTP response for this page *

**/

public void run(com.haht.project.Request aRequest, com.haht.project.Response aResponse) {

HtmlWriter out;

// user-defined run() local variables

try {

// Initialize variables. out = aResponse.getWriter();

// Custom initialization code executes before HTML // tags are generated.

// Output HTTP response headers. // Custom HTTP Response Headers.

aResponse.setContentType("text/html"); out.print("\r\n");

References to the Request and Response objects

The run method, seen in the example above, has two arguments:

• the Request object (aRequest), whose methods get information from the Web browser — for example, methods to read data passed in a form field or on the URL.

• the Response object (aResponse), whose methods send information to the Web server — for example, HTML output and cookies.

The Request and Response objects are built in to your HAHTsite application, along with the Application and Session objects. Both these object references (aRequest and aResponse) will be useful in your in-line code. For example, to read a value passed in a form or on the URL, you would call

aRequest.getURLField. // Generate HTML.

out.print("<HTML>\r\n"); out.print("<HEAD>\r\n");

out.print(" <META name=\"Generator\" content=\"HAHTsite 4.0\">\r\n");

out.print(" <TITLE>ShowDate</TITLE>\r\n"); out.print("</HEAD>\r\n");

out.print("<BODY>\r\n"); out.print(" <P>"); Date d = new Date(); out.print(d);

out.print("</P>\r\n");

out.print(" <P>&nbsp;</P>\r\n"); out.print("</BODY>\r\n");

out.print("</HTML>");

// All HTML has been generated. Put cleanup code here.

} // try

catch (java.lang.Throwable t) {

handleException(t, "HsShowDate", aRequest, aResponse); } // catch

Note - For information about using the variables and methods of the Application and Session objects, see “Calling a dynamic page from Java” on page 30. For general information on the HAHTsite Server Object Model’s built-in objects, see “Built-in objects” on page 90.

A reference for writing HTML output

The run method also gets a reference to the default HtmlWriter. To write HTML output programmatically, you can use statements like this:

out.print(d);

Of course, you can also intermix text and in-line code on a page. In the Java source, the text turns into out.print statements.

An exception handler

The run method of every dynamic page calls a general-purpose exception handler, handleException, to catch runtime errors and print an error message similar to this.

You can override this method or add other exception handlers to your code. A form handler

The Java source for each dynamic page contains a form handler that adapts to the contents of the page. At runtime, when a user submits a form, the form handler determines which button was clicked and calls the appropriate command handler.

Because this page doesn’t contain a form, it has no command handlers. If it did, you would see additional places in which you can customize the command handlers by writing your own event-handling code. For more information, see Chapter 8, “Data-Agent Programming.”

Places to add classes and methods

In Server-side Code view, you will see a number of marked sections where you can add classes, methods, and variables:

• public, protected, and private methods • static initializer code

• nested classes

• public, protected, and private member variables /**

* Entry point that is called via URL when a form is * submitted from this page.

*

* @param aRequest - HTTP request for this page * @param aResponse - HTTP response for this page *

**/

public void mapFormToCmd

(com.haht.project.Request aRequest, com.haht.project.Response aResponse) {

boolean bCmdHandlerCalled = false; if (! bCmdHandlerCalled)

{

getErrors().add("Received form command did not invoke a command-handler", "form-handler");

run (aRequest, aResponse); }

}

// user-defined public methods

// protected methods

Editing HahtApplication.java and HahtSession.java

In addition to one or more Page subclasses, your HAHTsite application includes a HahtApplication class and a HahtSession class, which you can see in your project window.

// private methods

// user-defined private methods

// static initializer code static

{

// Initialization code for user-defined static variables.

}

// nested classes

// user-defined nested classes

// public member variables // user-defined public variables

// protected member variables

protected HahtApplication hahtApplication; protected HahtSession hahtSession;

// user-defined protected variables

// private member variables // user-defined private variables

You can open these files in the code editor and add variables and methods to these classes. You can use HahtApplication to cache variables that may be shared by multiple sessions. Use HahtSession to store variables that apply across page boundaries.

A session recordset is a particularly powerful use of the HahtSession object. You can update a recordset on one page and use it to populate a form on another, without reaccessing the database.

Both HahtApplication and HahtSession contain event handlers that you can edit; see “Event handling in a Java project” on page 31.

For more information about these files, see “Built-in objects” on page 90. Note - Do not call Server Object Model methods from the

constructors in HahtApplication and HahtSession. The Server Object Model must be initialized first. Instead, edit the onStart and onEnd methods.

In document HAHTsite IDE Programming Guide (Page 39-47)