The com.ms.wfc.html package can also be used on the server to provide a programmatic model for generating HTML and sending it to the client page. Unlike the client-side Dynamic HTML model, the server-side model is static because the server Java class has no interaction with the client document. Instead, the server composes HTML elements and sends them off sequentially to the client as they are encountered in the HTML template if one is specified.
Although not fully dynamic, this is still a powerful server feature. For example, you can apply DhStyle attributes to all parts of some template HTML code and then generate vastly different looking pages by just changing the DhStyle attributes. You do not have to programmatically generate all the individual style changes. Another advantage is that you can use the same model for generating dynamic HTML for both client and server applications, thereby making the HTML generation easier to learn and remember.
There are currently two modes of generating HTML on the server. Both use Active Server Pages (ASP) scripting and a class based on the com.ms.wfc.html classes. The first is the "bare-bones" approach that relies more on the ASP script. The second uses a class derived from DhDocument and is very similar to the model that you use on the client because it places more control inside the class than in the script.
This approach uses two ASP methods on the server page: getObject and Response.Write. The getObject method is used to instantiate a class based on the WFC com.ms.wfc.html classes; the Response.Write method writes the generated HTML string to the client. The com.ms.wfc.html.DhElement class provides a getHTML method that creates the HTML string; this string is then sent to the client page using the ASP Response.Write method.
For example, you have a class called MyServer that extends DhForm and incorporates some HTML elements. In your ASP script, you first call getObject("java:MyServer") to create a DHTML object. You can then perform whatever actions you want on the object from your ASP script, such as setting properties on the object. When you have finished, you call the object's getHTML method to generate the string and pass that result to the ASP Response.Write method, which sends the HTML to the client. The following code fragments show the relevant ASP script and Java code for creating a DhEdit control in HTML and sending it to the client.
ASP SCRIPT
Dim f,x
set f = getObject( "java:dhFactory" )
set x= f.createEdit
x.setText( "I'm an edit!" )
Response.Write( x.getHTML() )
Response.Write( f.createBreak().getHTML() )
.
.
.
JAVA CODE
public class dhFactory {
public dhFactory(){ }
public DhBreak createBreak() {
return new DhBreak();
}
public DhEdit createEdit(){
return new DhEdit();
}
}
This approach is slightly more sophisticated and closer to the client model. It still uses an ASP script to site the DhDocument class, but the rest of the operational code is in Java. As in the client model, the DhModule class is instantiated as the Java component on the Web page and automatically calls the initForm method in your project class that derives from DhDocument.
As in the client model, you can do all your binding setup in your initForm call. The onDocumentLoad function is also called for your server-side class. In this method, you can access the IIS Response and Request objects (using the DhModule getResponse and getRequest methods) and also append new DhElement items to your document stream. However, it is important to understand that you cannot use document-level functions, such as findElement, or use enumeration operations on a server-side document, except on items that you have explicitly added to your DhDocument-derived class.
To use the HTML-based approach, follow these steps:
If you call setHTMLDocument with an empty string (""), your DhDocument class runs and outputs the HTML for any elements you have added at the location in your ASP where setHTMLDocument is called. You can then generate sections of HTML code inline.
If you do not call setHTMLDocument, the DhDocument class outputs full HTML for the page, including the <HTML>, <HEAD>, and <BODY> tags.
The following sample shows an ASP page that uses a template:
<% Set mod = Server.CreateObject( "com.ms.wfc.html.DhModule" )
mod.setCodeClass( "Class1" )
mod.setHTMLDocument( "c:\inetpub\wwwroot\Page1.htm" )
%>
At run time, the framework recognizes that your class is running on a server and acts accordingly.
Once instantiated, you can add elements or text to your DhDocument-derived class. Those items will be appended to any template specified just before the </BODY> tag.
The following sample demonstrates a class that works on either the client or the server.
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
public class Class1 extends DhDocument {
public Class1(){
initForm();
}
DhText txt1 = new DhText();
DhForm sect = new DhForm();
private void initForm() {
// call getServerMode() to check
// if this object is running on the server
if ( getServerMode() ){
txt1.setText( "Hello from the server!" );
}else{
txt1.setText( "Hello from the client!" );
}
// size the section, set its background color
// and add the txt1 element to it
sect.setSize( 100, 100 );
sect.setBackColor( Color.RED );
sect.add( txt1 );
add( sect );
setNewElements( new DhElement[] { sect } );
}
}
If you want to bind to an existing HTML document on the page, use the DhDocument.setBoundElements method, just as you would on the client. For example, if your HTML template contains the following HTML:
<P>
The time is:<SPAN id=txt1></SPAN><BR>
<INPUT type=text id=edit1 value=””>
</P>
Your initForm method looks like this:
DhText txt1 = new DhText();
DhEdit edit = new DhEdit();
DhComboBox cb = new DhComboBox();
private void initForm(){
txt1.setText(com.ms.wfc.app.Time().formatShortTime());
edit.setText(“Hello, world!”);
edit.setBackColor( Color.RED );
setBoundElements( new DhElement[]{ txt1.setBindID( “txt1” )
edit.setBindID( “edit1” ) } );
// Create a combo box to be added after the bound items
cb.addItem( “One” );
cb.addItem( “Two” );
// Add the items to the end of document.
setNewElements( new DhElement[]{ cb });
}
There are very few differences between the interpretation of server and client HTML classes. However, there is one important difference. Once elements are written (sent to the client), they cannot be modified as they can on a client document. The DhCantModifyElement exception, which is relevant only for server applications, is thrown after a write has been performed on an element if an attempt is made to modify that element again. (This underscores the fact that there is no real interoperation between the server Java class and the client document as there is on the client between the Java class and the document: from the server's standpoint, once written, the element is essentially gone.)
One advantage of using the DhDocument-derived method is that you can implement an HTML template that is embedded with attributes recognized by the com.ms.wfc.html classes. By first decorating the HTML elements in the file with ID attributes and then setting the corresponding IDs in the source code using the DhElement.setBindID method, you can bind to these HTML elements, set properties on the elements, add your own inline HTML code, and so forth. This essentially allows you to code and design separate templates ahead of time and populate the template with dynamic data when the document is requested from the server.