CS 596: Client-Server Programming
| 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 |
sdsu.net
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
public static void sendRedirect( String url )
Send url as redirect to client
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() );
}
}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" );
}
}
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()
<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@@
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 );
}
}
<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
| Button | Image |
| Form | SelectionList |
| Formatter | TextInput |
| HTMLTable | 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()
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() );
}
}
<!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>
checkBox
int windowSize = 4;
String name = "Fun";
SelectionList demo =
SelectionList.scrolling(name,windowSize);
dropDownMenu
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
formname 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.
Needs at least one submit button.
String url = "http://www.eli.sdsu.edu/cgiExamples/test.cgi"; Form get = Form.methodGet( url ); Form post = Form.methodPost( url );
append( ... ); appendLine( ... );
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 );
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()
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

