When you publish a project, for each dynamic page, the IDE generates a file (pagename.hbs) that includes a constructor and an entry point for
HS_pagename. The source code for each dynamic page has these contents. Note - Much of what you see in Server-side Code view for a HAHTtalk Basic page is generated for use by the IDE/IP’s dialog boxes and can be ignored. In the example that follows, ellipses mark code that has been omitted.
• A place for include files • Type definitions
• A page "constructor" • A form handler
• An entry point for the page • An exit handler
• An error handler
The following sections explain each of these topics, using as an example the HAHTtalk Basic source for a page that simply displays the current date. A place for include files
You can use the HAHTtalk Basic #include directive to include in a page one or more files that contain HAHTtalk Basic declarations. HAHTsite also supports a per-project Globals.hbs code page, which you can use to declare session “global” variables that can be accessed by all of the HAHTtalk Basic code in a project. (If this file is present in your project, its contents are automatically prepended to each .hbs file.) For more information, see “Include files and
Type definitions
The page defines two constants that can be useful in Print statements: Const HSQUOT = Chr$(34)
Const HSCRLF = Chr$(13) & Chr$(10)
HSQUOT produces quotation marks; use this constant when you want to embed quotation marks in a printed string. HSCRLF (carriage return/line feed) forces a line break in the HTML source file. (Remember that line breaks in HTML source files are ignored; if you want a line break in the displayed HTML, you will still need to add tags such as "<BR>" or "<P>...</P>".)
You can add your own public or private variables, functions, or subroutines in the marked places of this section. For example, you might want to add a subroutine to be called as the “success” or “failure” page from a database action of a form button on this page.
' Generated by HAHTsite
' Includes. Includes can only be at the top of the file, ' and should not have code in them.
' Imports and includes.
' Type definitions. ' Privates
' Publish-code-generation helper variables - do not remove! Private Haht_TempJavaObj As Object
' Used for static-finals from Java Classes, temp java objects Private Haht_Page as Object
Private HS_ShowDate_Construct_Called As Boolean ' User-defined private variables.
' More user-defined private variables.
' Private Functions and Subs
A page "constructor"
This code is called the first time this page is browsed in a session. You can add custom code to the constructor.
' More user-defined private functions and subroutines.
' Public variables
Const HSCRLF = Chr$(13) & Chr$(10) Const HSQUOT = Chr$(34)
' User-defined public variables.
' Public Functions and Subs
' User-defined public functions and subs.
' User-defined functions and subroutines.
' Constructor for page. Called once per page per state. Private Function HS_ShowDate_Construct() As Boolean On Error Goto HAHTErrorHandler_cons
Dim aResponse As Object
Set Haht_Page = Haht.getPage() HS_ShowDate_Construct = TRUE If (HS_ShowDate_Construct_Called) Then Exit Function Else HS_ShowDate_Construct_Called = TRUE End If ...
' Custom constructor code. This code executes ' the first time a page is called.
A form handler
If your page contains one or more forms with buttons, then the Type Definitions section of your code will contain this line:
#CONST HAHT_FORM_HANDLER_DEFINED=1
and the form handler code (enclosed by an #If statement) will execute. The generated code for a page’s form handler 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. In a HAHTtalk Basic project, you can create a custom form handler. You can also add your own code to process a Submit button, via the “User Code” choice in the button’s Action field. For more information, see Chapter 8, “Data-Agent Programming.”
An entry point for the page
#If (HAHT_FORM_HANDLER_DEFINED=1) Then ' Form handler
Public Sub HS_ShowDate_fH
Dim bCmdHandlerCalled As Boolean
Dim thePage As Object, theErrors As Object Dim aResponse As Object
Set aResponse = Haht.getResponse() On Error Goto HAHTErrorHandler_fH bCmdHandlerCalled = False
If (Not bCmdHandlerCalled) Then Set thePage = Haht.getPage()
Set theErrors = thePage.getErrors()
theErrors.add "Received form command did not _ invoke a command-handler", "form-handler" Set thePage = Nothing
Set theErrors = Nothing HS_ShowDate
End If
HAHTExitHandler_fH: Exit Sub
HAHTErrorHandler_fH:
aResponse.addHeaderLine "Content-type: text/html" Print ""
Print "<HTML><HEAD></HEAD><BODY><H3>"
Print "The following error has occurred on page _ HS_ShowDate on line " & Str$(erl);
If (Err.Source <> "") Then
Print " of script " & Err.Source; End If
Print ": <BR>" & Str$(Err) & " - " & Error & "<BR>"; Print "</H3></BODY></HTML>"
Resume HAHTExitHandler_fH End Sub
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 subroutine then sets up the basic structure of the page, which would look like this in HTML:
<HTML>
<HEAD>...</HEAD> <BODY>...</BODY> </HTML>
When you add in-line code to a page, it’s added here, between the <BODY> and </BODY> tags. (However, for information about output that must go in the HTTP headers, see “Writing headers” on page 126.)
' Entry point for page Sub HS_ShowDate() ' Local variables
Dim aResponse As Object Dim aRequest As Object Dim out As Object
On Error Goto HAHTErrorHandler
' Custom local variables. These are instantiated ' each time the page is run.
' Initialize local variables.
Set aResponse = Haht.getResponse() Set aRequest = Haht.getRequest() Set out = aResponse.getWriter()
If (Not HS_ShowDate_Construct()) Then ' Constructor failed - exit page Exit Sub
End If
' Custom initialization code. Executes before ' HTML tags are generated.
' Output HTTP response headers.
Object references you can use
The code shown above gets references to the Request and Response objects, as well as the default HtmlWriter. You can use aRequest and aResponse to read values sent by the Web server or to write HTML. For example, to read a value passed in a form or on the URL, you would call aRequest.getURLField. This code also gets a reference to the default HtmlWriter. To write HTML output programmatically, you simply call the Print statement. You can also intermix text and in-line code on a page. In the HAHTtalk Basic source, the text turns into Print statements.
For more information about these objects, see Chapter 5, “HAHTsite Server Object Model.”
aResponse.addHeaderLine "Content-type: text/html" Print HSCRLF
' More custom HTTP response headers. ' Add HTTP headers here.
' Generate HTML for browser. print "<HTML>" & HSCRLF _ & "<HEAD>" & HSCRLF _
& " <META name=" & HSQUOT & "Generator" & HSQUOT & _ "content=" & HSQUOT & "HAHTsite 4.0" & HSQUOT _ & ">" & HSCRLF _
& " <TITLE>ShowDate</TITLE>" & HSCRLF _ & "</HEAD>" & HSCRLF _
& "<BODY>" & HSCRLF _ & " <P>" ;
Print date$
print "</P>" & HSCRLF _ & "</BODY>" & HSCRLF _ & "</HTML>" ;
An error handler
Each dynamic page includes a default error handler and an On Error Goto statement that traps page-level errors. You can add code to the default error handler, and you can also include your own HAHTtalk Basic On Error construct in your subroutines. Your “On Error” construct will be nested inside the default error handler and will trap subroutine-level errors.