SDSU CS 596: Client-Server Programming
Spring Semester, 1997
Doc 22 SDSU CGI Classes

To Lecture Notes Index
San Diego State University -- This page last updated Apr 8, 1997
----------

Contents of Doc 22 SDSU CGI Classes


SDSU CGI Support Classes slide # 1
...sdsu.net.CGI slide # 2
...sdsu.test.InteractiveProperties slide # 4
Creating HTML in Java slide # 5
...Use sdsu.util.Template slide # 5
...Use sdsu.html slide # 8
......WebPage slide # 8
......Button slide # 10
......SelectionList slide # 11
......TextInput slide # 12
......Form slide # 13
......HTMLTable slide # 15
......Formatter slide # 16
CGI and Hinges slide # 17


Doc 22 SDSU CGI Classes Slide # 1
SDSU CGI Support Classes

The classes:

sdsu.net

CGI

sdsu.test
InteractiveProperties

sdsu.html
Button
Form
Formatter
HTMLTable
Image
SelectionList
TextInput
WebPage
sdsu.util
Template


Doc 22 SDSU CGI Classes Slide # 2

sdsu.net.CGI


public static CGI getInstance()
     Returns CGI object. Is singleton.

public Properties getFormData()
     Returns form data in Properties so can access as name
value pair
     Returns InteractiveProperties if program is not run by
webserver

public Properties getCGIEnvironmentVariables()
     Returns environment variables in Properties
     Returns InteractiveProperties if program is not run by
webserver


public String getBaseURL()
public String getFullURL()
     Returns URL of the CGI program

isGetRequest() 
isPostRequest() 
isPutRequest() 

public void sendText( String textMessage )
     Send a text message to the client
     Sends proper heading first time used

public void sendHtml( String htmlMessage )
     Send an html encoded message to the client.
     Sends proper heading first time used

Doc 22 SDSU CGI Classes Slide # 3
public static void sendRedirect( String url ) Send url as redirect to client

Doc 22 SDSU CGI Classes Slide # 4
Example use of sdsu.net.CGI -
Simple CGIEcho.java


class SimpleCgiEcho
     {
     public  static  void  main( String  args[] )
          {
          CGI echo = CGI.getInstance();

          Properties formData = echo.getFormData();

          StringBuffer message = new StringBuffer();
          
          message.append( "Here are all the form name-value
pairs" );
          
          Enumeration keys = formData.keys();
          while ( keys.hasMoreElements() )
               {
               String currentKey = (String) keys.nextElement();
               message.append( currentKey);
               message.append( " = ");
               message.append( formData.get( currentKey ));
               message.append( "\n");
               }
               
          echo.sendText( message.toString() );
          }
     }



Doc 22 SDSU CGI Classes Slide # 5

sdsu.test.InteractiveProperties


This Properties acts like a normal hashtable or Properties, except when a key or value is not found on a lookup.

In this case, the hashtable prompts the user via standard in for the value in question.


class TestInteractiveProperties
     {
     public  static  void  main( String  args[] )
          {
          InteractiveProperties askMe = new
InteractiveProperties();
          askMe.contains( "me" );
          askMe.get( "money" );
          }
     }


Doc 22 SDSU CGI Classes Slide # 6

Creating HTML in Java

First Method - Don't

Use sdsu.util.Template


The methods
public static Template fromFile( String fileName )

public Template( String templateText )

public void replace( String variableName, HTML variableValue
)

public void replace( String variableName, String variableValue
)

public void setVariableMarker( String newMarker )

public String toString()


Doc 22 SDSU CGI Classes Slide # 7
Example

Create a file with HTML: say payUp

<HTML>
<HEAD>
<TITLE>Collection Agency</TITLE>
</HEAD>
<BODY>
<H2>
<center>Time To Pay Up</center></H2>
<P>
Dear @@@deadBeat@@@;
<P>According to our records you owe us
@@@amount@@@. 
Pay now or <STRONG>else</STRONG>!

Sincerely
     @@@me@@

Program
class ShowTemplate
     {
     public  static  void  main( String  args[] ) throws Exception
          {
          Template overDue = Template.fromFile( "payUp" ) ;
          overDue.replace( "deadBeat", "George" );
          overDue.replace( "amount", "$1,000,000" );
          overDue.replace( "me", "Nice Guy" );
          System.out.println( overDue );
          }
     }


Doc 22 SDSU CGI Classes Slide # 8
Output

<HTML>
<HEAD>
<TITLE>Collection Agency</TITLE>
</HEAD>
<BODY>
<H2>
<center>Time To Pay Up</center></H2>
<P>
Dear George;
<P>According to our records you owe us $1,000,000. 
Pay now or <STRONG>else</STRONG>!

Sincerely
Nice Guy


Doc 22 SDSU CGI Classes Slide # 9
Creating HTML in Java

First Method - Use sdsu.html


ButtonImage
FormSelectionList
FormatterTextInput
HTMLTableWebPage

WebPage

WebPage() 
WebPage( String pageTitle )

append( Object )
appendLineBreak() 

clearPageBody() 

getPageBody() 
getPageEnding() 
getPageHeader() 
     Returns in HTML format the header information of the
page. 

setBackgroundColor(String color ) 
setBackgroundImage(String imageUrl) 
setTextColor(String color ) 

toString() 

Doc 22 SDSU CGI Classes Slide # 10
Example

class SimplePage
     {
     public  static  void  main( String  args[] )
          {
          WebPage simple = new WebPage( "Example" );
          simple.setTextColor( WebPage.WHITE);
          simple.setBackgroundColor( WebPage.BLACK);
          simple.append( "Hi Mom");
          
          CGI myPage = CGI.getInstance();
          myPage.sendHtml( simple.toString() );
          }
     }

HTML Output
<!DOCTYPE HTML SYSTEM "html.dtd">
<!-- Document generated via code written using java library sdsu.html version
0.8>
<HTML>
<HEAD>
<TITLE>Example
</TITLE>
</HEAD>
<BODY TEXT="#FFFFFF" BGCOLOR="#000000">Hi
Mom
</BODY>
</HTML>

Doc 22 SDSU CGI Classes Slide # 11

Button


All buttons must be in a form

checkBox

Has name and value
If checked returns name=value
hidden
Store name-value pair hidden from users view

radio
Has name and value
All radio buttons with same name are in same group
Selected button returns name=value

reset
Returns form to original state when pressed

submit
Sends form values to form URL

Doc 22 SDSU CGI Classes Slide # 12

SelectionList


scrolling
Shows items in a scrolling window
Has a name

     int windowSize = 4;
     String name = "Fun";
     SelectionList demo =
SelectionList.scrolling(name,windowSize);

dropDownMenu

Shows items in a drop down menu
Has a name

     String name = "drop";
     SelectionList demo =
SelectionList.dropDownMenu(name);


Adding items to list

     String itemNameAndReturnValue = "Hi";
     String itemName = "Bye";
     String itemReturnValue = "a";

     demo.append( itemNameAndReturnValue );
     demo.append( itemName, itemReturnValue );

List name and return value of selected item is returned with form

Doc 22 SDSU CGI Classes Slide # 13

TextInput


Must be in a form

name is set to CGI program, its value is the text in the input area

password(name, numberOfColumns) 
     Create a text input area for entering passwords. 

scrolling(name, numberOfColumns, numberOfRows) 
     Create a scrolling text input area. 

setInitalText(String) 
     Sets the inital text seen in the text input. 

singleLine(name, numberOfColumns) 
     Create a single-line text input area. 

singleLine(name, numberOfColumns) 
     Create a single-line text input area. 



Doc 22 SDSU CGI Classes Slide # 14

Form


A container for Buttons, SelectionLists, TextInputs, and any other HTML items/tags you wish to add.

Needs at least one submit button.

Creation

String url = "http://www.eli.sdsu.edu/cgiExamples/test.cgi";

Form get = Form.methodGet( url );
Form post = Form.methodPost( url );


Methods

append( ... );
appendLine( ... );


Doc 22 SDSU CGI Classes Slide # 15
Example

Form simple = Form.methodGet("http://www.eli.sdsu.edu/cgi
bin/cgiExamples/CGIEcho");

// Show some Button examples
simple.appendLine( "Programmer Desire" );
simple.appendLine( Button.radio( "desire", "low"), "Low");
simple.appendLine( Button.radio( "desire", "med"),
"Meduim");
simple.appendLine( Button.radio( "desire", "high"), "High");
simple.append( Button.hidden( "suprise", "can't see me"));
simple.appendLine();

// Show some TextInput examples
simple.appendLine( "Please type in your name",
TextInput.singleLine( "name", 8 ));

// Show some SelectionList examples
SelectionList language =  SelectionList.scrolling( "language",
4);
language.append( "C++", "a" );
language.append( "C");
language.append( "Java");
language.append( "Ada" );
language.append( "Pascal" );

simple.append( Button.reset( "Clear Form"));
simple.append( Button.submit( "Send Now"));

WebPage test = new WebPage( "Form Test");
test.append( simple );

Doc 22 SDSU CGI Classes Slide # 16

Doc 22 SDSU CGI Classes Slide # 17

HTMLTable

HTMLTable(int, int) 
     Create a new table with given number of rows and columns. 
HTMLTable(Table) 
     Create a new html table with element given by the
sdsu.util.Table 


alignCenter() 
alignLeft() 
alignRight() 

elementAt(int, int) 

makeColumnAHeader(int) 
makeRowAHeader(int) 

setBorderWidth(int) 
setCaption(Formatter) 
     Sets text caption displayed above the table. 

setElementAt(HTML, int, int) 
setElementAt(Object, int, int) 

setWidth(int) 
     Suggests a width in pixels of the table. 

setWidthPercent(int) 
     Suggests a width for the table in percent of window width. 

toString() 


Doc 22 SDSU CGI Classes Slide # 18

Formatter


Formats text and other items

alignCenter()      alignLeft()      alignRight() 

append(HTML) 
     Append the an existing HTML object to the current end of
the text. 

append(String) 
appendBold(String) 
     Append the string to the current end of the text as bold text 

appendHeading(String, int) 
appendHTMLTags(String) 
     Appends the string without escaping special characters. 
appendItalic(String) 
appendLineBreak() 
appendLink(String, url) 
     Append the string to the current end of the text as linked text. 
appendMailLink(String) 
     Appends a mailTo reference. 

makeBlockQuote() 
makeParagraph() 
makePreformatted() 

toString() 
     Converts the text to an string with proper html tags 


Doc 22 SDSU CGI Classes Slide # 19

CGI and Hinges









----------