Example: Using the HTML form classes
The following example shows you how to use the HTML form classes.
You can also view a sample output from
running this code. The HTML classes used in the "showHTML" method are
bold.
/////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////
//
// This source is an example of using the AS/400 Toolbox for Java HTML
// package classes, which allow you to easily build HTML Forms.
// IBM grants you a nonexclusive license to use this as an example
// from which you can generate similar function tailored to
// your own specific needs.
//
// This sample code is provided by IBM for illustrative purposes
// only. These examples have not been thoroughly tested under all
// conditions. IBM, therefore, cannot guarantee or imply
// reliability, serviceability, or function of these programs.
//
// All programs contained herein are provided to you "AS IS"
// without any warranties of any kind. The implied warranties of
// merchantablility and fitness for a particular purpose are
// expressly disclaimed.
//
// AS/400 Toolbox for Java
// (C) Copyright IBM Corp. 1997, 1999
// All rights reserved.
// US Government Users Restricted Rights -
// Use, duplication, or disclosure restricted
// by GSA ADP Schedule Contract with IBM Corp.
//
/////////////////////////////////////////////////////////////////////////
package customer;
import java.io.*;
import java.awt.Color;
import javax.servlet.*;
import javax.servlet.http.*;
import com.ibm.as400.access.*;
import com.ibm.as400.util.html.*;
public class HTMLExample extends HttpServlet
{
private static boolean found = false; // Determines if user already exists in
// the list of registrants.
String regPath = "c:\\registration.txt"; // Registration information will be stored here
public void init(ServletConfig config)
{
try
{
super.init(config);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Process the GET request.
* @param req The request.
* @param res The response.
**/
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
// Display the Web using the new HTML classes
out.println(showHTML());
out.close();
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String nameStr = req.getParameter("name");
String emailStr = req.getParameter("email");
String errorText= "";
// Output stream to write to the servlet
ServletOutputStream out = res.getOutputStream();
res.setContentType("text/html");
// Check name & e-mail parameters for valid values
if (nameStr.length() == 0)
errorText += "Customer Name not entered. ";
if (emailStr.length() == 0)
errorText += "E-mail not entered. " ;
// If name & e-mail have both been provided, continue.
if (errorText.length() == 0)
{
try
{
//Create the registration.txt file
FileWriter f = new FileWriter(regPath, true);
BufferedWriter output = new BufferedWriter(f);
//buffered reader for searching the file
BufferedReader in = new BufferedReader(new FileReader(regPath));
String line = in.readLine();
// reset the found flag
found = false;
// Check to see if this customer has already registered
// or has already used the same e-mail address
while (!found)
{
// if file is empty or end of file reached.
if (line == null)
break;
// if customer already registered
if ((line.equals("Customer Name: " + nameStr)) || (line.equals("Email address: " + emailStr)))
{
// Output a message to the customer saying they have already
// registered
out.println ("<HTML> " +
"<TITLE> Toolbox Registration</TITLE> " +
"<META HTTP-EQUIV=\"pragma\" content=\"no-cache\"> " +
"<BODY BGCOLOR=\"blanchedalmond\" TEXT=\"black\"> " );
out.println ("<P><HR>" +
"<P>" + nameStr + "</B>, you have already registered using that " +
"<B>Name</B> or <B>E-mail address</B>." +
"<P> Thank You!...<P><HR>");
// Create a HTMLHyperlink object and display it
out.println ("<UL><LI>" + new HTMLHyperlink("./customer.HTMLExample", "Back to Registration Form") + "</UL></BODY></HTML>");
found = true;
break;
}
else // read the next line
line = in.readLine();
}
// String object to hold data submitted from the HTML Form
String data;
// If the users name or e-mail aren't found in our text file, continue.
if (!found)
{
//----------------------------------------------------------------------------------
// Insert the new customer info into a file
output.newLine();
output.write("Customer Name: " + nameStr);
output.newLine();
output.write("Email address: " + emailStr);
output.newLine();
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "USE" checkbox from form
data = req.getParameter("use");
if(data != null)
{
output.write("Currently Using Toolbox: " + data);
output.newLine();
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "More Information" checkbox from form
data = req.getParameter("contact");
if (data != null)
{
output.write("Requested More Information: " + data);
output.newLine();
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "AS400 Version" from form
data = req.getParameter("version");
if (data != null)
{
if (data.equals("multiple versions"))
{
data = req.getParameter("MultiList");
output.write("Multiple Versions: " + data);
}
else
output.write("AS400 Version: " + data);
output.newLine();
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "Current Projects" from form
data = req.getParameter("interest");
if (data != null)
{
output.write("Using Java or Interested In: " + data);
output.newLine();
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "Platforms" from form
data = req.getParameter("platform");
if (data != null)
{
output.write("Platforms: " + data);
output.newLine();
if (data.indexOf("Other") >= 0)
{
output.write("Other Platforms: " + req.getParameter("OtherPlatforms"));
output.newLine();
}
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "Number of AS/400's" from form
data = req.getParameter("list1");
if (data != null)
{
output.write("Number of AS/400's: " + data);
output.newLine();
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "Comments" from form
data = req.getParameter("comments");
if (data != null && data.length() > 0)
{
output.write("Comments: " + data);
output.newLine();
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting "Attachment"
data = req.getParameter("myAttachment");
if (data != null && data.length() > 0)
{
output.write("Attachment File: " + data);
output.newLine();
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//Getting Hidden "Copyright" infomation
data = req.getParameter("copyright");
if (data != null)
{
output.write(data);
output.newLine();
}
//----------------------------------------------------------------------------------
output.flush();
output.close();
// Print a thanks to the customer
out.println("<HTML>");
out.println("<TITLE>Thank You!</TITLE>");
out.println("<META HTTP-EQUIV=\"pragma\" content=\"no-cache\"> ");
out.println("<BODY BGCOLOR=\"blanchedalmond\">");
out.println("<HR><P>Thank You for Registering, <B>" + nameStr + "</B>!<P><HR>");
// Create a HTMLHyperlink object and display it
out.println("<UL><LI>" + new HTMLHyperlink("./customer.HTMLExample", "Back to Registration Form"));
out.println("</UL></BODY></HTML>");
}
}
catch (Exception e)
{
// Show error in browser
out.println("<HTML>");
out.println("<TITLE>ERROR!</TITLE>");
out.println("<META HTTP-EQUIV=\"pragma\" content=\"no-cache\"> ");
out.println("<BODY BGCOLOR=\"blanchedalmond\">");
out.println("<BR><B>Error Message:</B><P>");
out.println(e + "<P>");
// Create a HTMLHyperlink object and display it
out.println("<UL><LI>" + new HTMLHyperlink("./customer.HTMLExample", "Back to Registration Form"));
out.println("</UL></BODY></HTML>");
e.printStackTrace();
}
}
else
{
// Output a message to the customer saying customer name & e-mail not entered. Please
// try again
out.println ("<HTML> " +
"<TITLE>Invalid Registration Form</TITLE> " +
"<META HTTP-EQUIV=\"pragma\" content=\"no-cache\"> " +
"<BODY BGCOLOR=\"blanchedalmond\" TEXT=\"black\"> " );
out.println ("<HR><B>ERROR</B> in customer data - <P><B>" +
errorText + "</B><P> Please Try Again... <HR>");
// Create a HTMLHyperlink object and display it
out.println("<UL><LI>" + new HTMLHyperlink("./customer.HTMLExample", "Back to Registration Form") + "</UL></BODY></HTML>");
}
// Close the writer
out.close();
}
public void destroy(ServletConfig config)
{
// do nothing
}
public String getServletInfo()
{
return "My Product Registration";
}
private String showHTML()
{
// String Buffer to hold HTML Page
StringBuffer page = new StringBuffer();
// Create the HTML Form object
HTMLForm form = new HTMLForm("/servlet/customer.HTMLExample");;
HTMLText txt;
// Build the beginning of the HTML Page and add it to the String Buffer
page.append("<HTML>\n");
page.append("<TITLE> Welcome!!</TITLE>\n");
page.append("<HEAD><SCRIPT LANGUAGE=\"JavaScript\">function test(){alert(\"This is a sample script executed with a ButtonFormInput.\")}</SCRIPT></HEAD>");
page.append("<META HTTP-EQUIV=\"pragma\" content=\"no-cache\">\n");
page.append("<BODY BGCOLOR=\"blanchedalmond\" TEXT=\"black\"><BR>\n");
try
{
//------------------------------------------------------------------------------------------
// Create page title using HTML Text
txt = new HTMLText("Product Registration");
txt.setSize(5);
txt.setBold(true);
txt.setColor(new Color(199, 21, 133));
txt.setAlignment(HTMLConstants.CENTER);
// Add HTML Text to the String Buffer
page.append(txt.getTag(true) + "<HR><BR>\n");
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create a Line Layout
LineLayoutFormPanel line = new LineLayoutFormPanel();
txt = new HTMLText("Enter your name and e-mail address:");
txt.setSize(4);
line.addElement(txt);
// Add the Line Layout to String Buffer
page.append(line.toString());
page.append("<BR>");
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Set the HTML Form METHOD
form.setMethod(HTMLForm.METHOD_POST);
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create a Text input for the name.
TextFormInput user = new TextFormInput("name");
user.setSize(25);
user.setMaxLength(40);
// Create a Text input for the email address.
TextFormInput email = new TextFormInput("email");
email.setSize(30);
email.setMaxLength(40);
// Create a ImageFormInput
ImageFormInput img = new ImageFormInput("Submit Form", "..\\images\\myPic.gif");
img.setAlignment(HTMLConstants.RIGHT);
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create a LineLayoutFormPanel object for the name & e-mail address
LineLayoutFormPanel line2 = new LineLayoutFormPanel();
// Add elements to the line form
line2.addElement(new LabelFormElement("Name:"));
line2.addElement(user);
// Create and add a Label Element to the Line Layout
line2.addElement(new LabelFormElement("E-mail:"));
line2.addElement(email);
line2.addElement(img);
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create Questions line layout
LineLayoutFormPanel line3 = new LineLayoutFormPanel();
// Add elements to the line layout
line3.addElement(new LineLayoutFormPanel());
line3.addElement(new CheckboxFormInput("use", "yes", "Do you currently use the Toolbox?", false));
line3.addElement(new LineLayoutFormPanel());
line3.addElement(new CheckboxFormInput("contact", "yes", "Would you like information on future Toolbox releases?", true));
line3.addElement(new LineLayoutFormPanel());
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create Version Radio Group
RadioFormInputGroup group = new RadioFormInputGroup("version");
// Add Radio Form Inputs to the Group
group.add(new RadioFormInput("version", "v3r2", "V3R2", false));
group.add(new RadioFormInput("version", "v4r1", "V4R1", false));
group.add(new RadioFormInput("version", "v4r2", "V4R2", false));
group.add(new RadioFormInput("version", "v4r3", "V4R3", false));
group.add(new RadioFormInput("version", "v4r4", "V4R4", false));
group.add(new RadioFormInput("version", "multiple versions", "Multiple Versions? Which ones:", false));
//Create a Select Form Element
SelectFormElement mlist = new SelectFormElement("MultiList");
mlist.setMultiple(true);
mlist.setSize(3);
//Create the Options for the Select Form Element
SelectOption option1 = mlist.addOption("V3R2", "v3r2");
SelectOption option2 = mlist.addOption("V4R1", "v4r1");
SelectOption option3 = mlist.addOption("V4R2", "v4r2");
SelectOption option4 = mlist.addOption("V4R3", "v4r3");
SelectOption option5 = mlist.addOption("V4R4", "v4r4");
// Create HTML text
txt = new HTMLText("Current AS/400 Level:");
txt.setSize(4);
// Create Grid Layout
GridLayoutFormPanel grid1 = new GridLayoutFormPanel(3);
// Add radio group & select form element to the grid
grid1.addElement(txt);
grid1.addElement(group);
grid1.addElement(mlist);
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create Grid Layout for interests
GridLayoutFormPanel grid2 = new GridLayoutFormPanel(1);
txt = new HTMLText("Current Projects or Area of Interest: (check all that apply)");
txt.setSize(4);
// Add elements to Grid Layout
grid2.addElement(new LineLayoutFormPanel());
grid2.addElement(txt);
// Create and add a Checkbox to the Grid Layout
grid2.addElement(new CheckboxFormInput("interest", "applications", "Applications", true));
grid2.addElement(new CheckboxFormInput("interest", "applets", "Applets", false));
grid2.addElement(new CheckboxFormInput("interest", "servlets", "Servlets", false));
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create Line Layout for platforms
LineLayoutFormPanel line4 = new LineLayoutFormPanel();
txt = new HTMLText("Client Platforms Used: (check all that apply)");
txt.setSize(4);
// Add elements to Line Layout
line4.addElement(new LineLayoutFormPanel());
line4.addElement(txt);
line4.addElement(new LineLayoutFormPanel());
line4.addElement(new CheckboxFormInput("platform", "95", "Windows95", false));
line4.addElement(new CheckboxFormInput("platform", "98", "Windows98", false));
line4.addElement(new CheckboxFormInput("platform", "NT", "WindowsNT", false));
line4.addElement(new CheckboxFormInput("platform", "OS2", "OS/2", false));
line4.addElement(new CheckboxFormInput("platform", "AIX", "AIX", false));
line4.addElement(new CheckboxFormInput("platform", "Linux", "Linux", false));
line4.addElement(new CheckboxFormInput("platform", "AS400", "AS/400", false));
line4.addElement(new CheckboxFormInput("platform", "Other", "Other:", false));
TextFormInput other = new TextFormInput("OtherPlatforms");
other.setSize(20);
other.setMaxLength(50);
line4.addElement(other);
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create a Line Layout for number of AS/400s
LineLayoutFormPanel grid3 = new LineLayoutFormPanel();
txt = new HTMLText("How many AS/400 do you have? ");
txt.setSize(4);
// Create a Select Form Element for number of AS/400's owned
SelectFormElement list = new SelectFormElement("list1");
// Create and add the Select Options to the Select Form Element List
SelectOption opt0 = list.addOption("0", "zero");
SelectOption opt1 = list.addOption("1", "one", true);
SelectOption opt2 = list.addOption("2", "two");
SelectOption opt3 = list.addOption("3", "three");
SelectOption opt4 = list.addOption("4", "four");
SelectOption opt5 = new SelectOption("5+", "FiveOrMore", false);
list.addOption(opt5);
// Add Elements to the Grid Layout
grid3.addElement(new LineLayoutFormPanel());
grid3.addElement(txt);
grid3.addElement(list);
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create a Grid Layout for Product Comments
GridLayoutFormPanel grid4 = new GridLayoutFormPanel(1);
txt = new HTMLText("Product Comments:");
txt.setSize(4);
// Add elements to the Grid Layout
grid4.addElement(new LineLayoutFormPanel());
grid4.addElement(txt);
//grid4.addElement(new LineLayoutFormPanel());
// Create a Text Area Form
grid4.addElement(new TextAreaFormElement("comments", 5, 75));
grid4.addElement(new LineLayoutFormPanel());
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Create a Grid Layout
GridLayoutFormPanel grid5 = new GridLayoutFormPanel(2);
txt = new HTMLText("Would you like to sign onto an AS/400?");
txt.setSize(4);
// Create a Text input and Label for the system name.
TextFormInput sys = new TextFormInput("system");
LabelFormElement sysLabel = new LabelFormElement("System:");
// Create a Text input and Label for the userid.
TextFormInput uid = new TextFormInput("uid");
LabelFormElement uidLabel = new LabelFormElement("UserID");
// Create a Password input and Label for the password.
PasswordFormInput pwd = new PasswordFormInput("pwd");
LabelFormElement pwdLabel = new LabelFormElement("Password");
// Add the Text inputs, password inputs, and Labels to the grid
grid5.addElement(sysLabel);
grid5.addElement(sys);
grid5.addElement(uidLabel);
grid5.addElement(uid);
grid5.addElement(pwdLabel);
grid5.addElement(pwd);
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Add the various panels created to the HTML Form in the order you wish them to appear
form.addElement(line2);
form.addElement(line3);
form.addElement(grid1);
form.addElement(grid2);
form.addElement(line4);
form.addElement(grid3);
form.addElement(grid4);
form.addElement(txt);
form.addElement(new LineLayoutFormPanel());
form.addElement(grid5);
form.addElement(new LineLayoutFormPanel());
form.addElement(new HTMLText("Submit an attachment Here: <br />"));
// Add a File Input to the form
form.addElement(new FileFormInput("myAttachment"));
form.addElement(new ButtonFormInput("button", "TRY ME!", "test()"));
// Adds a empty Line Layout, which in turn
// adds a line break <br /> to the form
form.addElement(new LineLayoutFormPanel());
form.addElement(new LineLayoutFormPanel());
form.addElement(new SubmitFormInput("submit", "Register"));
form.addElement(new LineLayoutFormPanel());
form.addElement(new LineLayoutFormPanel());
form.addElement(new ResetFormInput("reset", "Reset"));
// Add a Hidden Input to the form
form.addElement(new HiddenFormInput("copyright", "(C) Copyright IBM Corp. 1999, 1999"));
//------------------------------------------------------------------------------------------
// Add the entire HTML Form to the String Buffer
page.append(form.toString());
}
catch(Exception e)
{
e.printStackTrace();
}
// Add the Ending HTML tags to the Buffer
page.append("</BODY>\n");
page.append("</HTML>\n");
// Return the entire HTML page string
return page.toString();
}
}