ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahh_5.4.0.1/htmfrmex.htm

705 lines
30 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Using the HTML form classes" />
<meta name="abstract" content="" />
<meta name="description" content="" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="htmfrmex" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Using the HTML form classes</title>
</head>
<body id="htmfrmex"><a name="htmfrmex"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using the HTML form classes</h1>
<div><p></p>
<div class="section"><p>The following example shows you how to use the HTML form classes.
You can also view a <a href="htmlout.htm#htmlout">sample output</a> from
running this code. The HTML classes used in the "showHTML" method are <strong>bold</strong>.</p>
<div class="note"><span class="notetitle">Note:</span> Read
the <a href="codedisclaimer.htm#codedisclaimer">Code example disclaimer</a> for
important legal information.</div>
<pre>///////////////////////////////////////////////////////////////////////////////
//
// This source is an example of using the IBM Toolbox for Java HTML
// package classes, which allow you to easily build HTML Forms.
//
///////////////////////////////////////////////////////////////////////////////
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
{
// Determines if user already exists in the list of registrants.
private static boolean found = false;
// Registration information will be stored here
String regPath = "c:\\registration.txt";
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 &amp; 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 &amp; 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 ("&lt;HTML&gt; " +
"&lt;TITLE&gt; Toolbox Registration&lt;/TITLE&gt; " +
"&lt;META HTTP-EQUIV=\"pragma\" content=\"no-cache\"&gt; " +
"&lt;BODY BGCOLOR=\"blanchedalmond\" TEXT=\"black\"&gt; " );
out.println ("&lt;P&gt;&lt;HR&gt;" +
"&lt;P&gt;" + nameStr +
"&lt;/B&gt;, you have already registered using that " +
"&lt;B&gt;Name&lt;/B&gt; or &lt;B&gt;E-mail address&lt;/B&gt;." +
"&lt;P&gt; Thank You!...&lt;P&gt;&lt;HR&gt;");
// Create a HTMLHyperlink object and display it
out.println ("&lt;UL&gt;&lt;LI&gt;" +
new HTMLHyperlink("./customer.HTMLExample",
"Back to Registration Form") +
"&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;");
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") &gt;= 0)
{
output.write("Other Platforms: " + req.getParameter("OtherPlatforms"));
output.newLine();
}
}
//------------------------------------------------------------
//------------------------------------------------------------
//Getting "Number of iSeries servers" from form
data = req.getParameter("list1");
if (data != null)
{
output.write("Number of iSeries servers: " + data);
output.newLine();
}
//------------------------------------------------------------
//------------------------------------------------------------
//Getting "Comments" from form
data = req.getParameter("comments");
if (data != null &amp;&amp; data.length() &gt; 0)
{
output.write("Comments: " + data);
output.newLine();
}
//------------------------------------------------------------
//------------------------------------------------------------
//Getting "Attachment"
data = req.getParameter("myAttachment");
if (data != null &amp;&amp; data.length() &gt; 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("&lt;HTML&gt;");
out.println("&lt;TITLE&gt;Thank You!&lt;/TITLE&gt;");
out.println("&lt;META HTTP-EQUIV=\"pragma\" content=\"no-cache\"&gt; ");
out.println("&lt;BODY BGCOLOR=\"blanchedalmond\"&gt;");
out.println("&lt;HR&gt;&lt;P&gt;Thank You for Registering, &lt;B&gt;" + nameStr + "&lt;/B&gt;!&lt;P&gt;&lt;HR&gt;");
// Create a HTMLHyperlink object and display it
out.println("&lt;UL&gt;&lt;LI&gt;" +
new HTMLHyperlink("./customer.HTMLExample", "Back to Registration Form"));
out.println("&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;");
}
}
catch (Exception e)
{
// Show error in browser
out.println("&lt;HTML&gt;");
out.println("&lt;TITLE&gt;ERROR!&lt;/TITLE&gt;");
out.println("&lt;META HTTP-EQUIV=\"pragma\" content=\"no-cache\"&gt; ");
out.println("&lt;BODY BGCOLOR=\"blanchedalmond\"&gt;");
out.println("&lt;BR&gt;&lt;B&gt;Error Message:&lt;/B&gt;&lt;P&gt;");
out.println(e + "&lt;P&gt;");
// Create a HTMLHyperlink object and display it
out.println("&lt;UL&gt;&lt;LI&gt;" +
new HTMLHyperlink("./customer.HTMLExample", "Back to Registration Form"));
out.println("&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;");
e.printStackTrace();
}
}
else
{
// Output a message to the customer saying customer name &amp;
// e-mail not entered. Please try again
out.println ("&lt;HTML&gt; " +
"&lt;TITLE&gt;Invalid Registration Form&lt;/TITLE&gt; " +
"&lt;META HTTP-EQUIV=\"pragma\" content=\"no-cache\"&gt; " +
"&lt;BODY BGCOLOR=\"blanchedalmond\" TEXT=\"black\"&gt; " );
out.println ("&lt;HR&gt;&lt;B&gt;ERROR&lt;/B&gt; in customer data - &lt;P&gt;&lt;B&gt;" +
errorText +
"&lt;/B&gt;&lt;P&gt;Please Try Again... &lt;HR&gt;");
// Create a HTMLHyperlink object and display it
out.println("&lt;UL&gt;&lt;LI&gt;" +
new HTMLHyperlink("./customer.HTMLExample", "Back to Registration Form") +
"&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;");
}
// 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
<strong>HTMLForm form = new HTMLForm("/servlet/customer.HTMLExample");;</strong>
<strong>HTMLText txt;</strong>
// Build the beginning of the HTML Page and add it to the String Buffer
page.append("&lt;HTML&gt;\n");
page.append("&lt;TITLE&gt; Welcome!!&lt;/TITLE&gt;\n");
page.append("&lt;HEAD&gt;&lt;SCRIPT LANGUAGE=\"JavaScript\"&gt;
function test(){alert(\"This is a sample script executed with a
ButtonFormInput.\")}&lt;/SCRIPT&gt;&lt;/HEAD&gt;");
page.append("&lt;META HTTP-EQUIV=\"pragma\" content=\"no-cache\"&gt;\n");
page.append("&lt;BODY BGCOLOR=\"blanchedalmond\" TEXT=\"black\"&gt;&lt;BR&gt;\n");
try
{
//--------------------------------------------------------------------
// Create page title using HTML Text
<strong>txt = new HTMLText("Product Registration");</strong>
<strong>txt.setSize(5);</strong>
<strong>txt.setBold(true);
txt.setColor(new Color(199, 21, 133));
txt.setAlignment(HTMLConstants.CENTER);</strong>
// Add HTML Text to the String Buffer
page.append(txt.getTag(true) + "&lt;HR&gt;&lt;BR&gt;\n");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Create a Line Layout
LineLayoutFormPanel line = new LineLayoutFormPanel();
<strong>txt = new HTMLText("Enter your name and e-mail address:");
txt.setSize(4);</strong>
line.addElement(txt);
// Add the Line Layout to String Buffer
page.append(line.toString());
page.append("&lt;BR&gt;");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Set the HTML Form METHOD
<strong>form.setMethod(HTMLForm.METHOD_POST);</strong>
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// 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\\myPiimages/c.gif");
img.setAlignment(HTMLConstants.RIGHT);
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Create a LineLayoutFormPanel object for the name &amp; 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
<strong>txt = new HTMLText("Current Server Level:");
txt.setSize(4);</strong>
// Create Grid Layout
GridLayoutFormPanel grid1 = new GridLayoutFormPanel(3);
// Add radio group &amp; 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);
<strong>txt = new HTMLText("Current Projects or Area of Interest: " +
"(check all that apply)");
txt.setSize(4);</strong>
// 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();
<strong>txt = new HTMLText("Client Platforms Used: " +
"(check all that apply)");
txt.setSize(4);</strong>
// 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",
"iSeries",
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 servers
LineLayoutFormPanel grid3 = new LineLayoutFormPanel();
<strong>txt = new HTMLText(
"How many iSeries™ servers do you have?");
txt.setSize(4);</strong>
// Create a Select Form Element for number of servers 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);
<strong>txt = new HTMLText("Product Comments:");
txt.setSize(4);</strong>
// Add elements to the Grid Layout
grid4.addElement(new LineLayoutFormPanel());
grid4.addElement(txt);
// 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);
<strong>txt = new HTMLText("Would you like to sign on to a server?");
txt.setSize(4);</strong>
// 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
<strong>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: &lt;br /&gt;"));</strong>
// Add a File Input to the form
<strong>form.addElement(new FileFormInput("myAttachment"));
form.addElement(new ButtonFormInput("button",
"TRY ME!",
"test()"));</strong>
// Adds a empty Line Layout, which in turn
// adds a line break &lt;br /&gt; to the form
<strong>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"));</strong>
// Add a Hidden Input to the form
<strong>form.addElement(new
HiddenFormInput("copyright",
"(C) Copyright IBM<sup>®</sup> Corp. 1999, 1999"));</strong>
//--------------------------------------------------------------------
// 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("&lt;/BODY&gt;\n");
page.append("&lt;/HTML&gt;\n");
// Return the entire HTML page string
return page.toString();
}
}</pre>
</div>
</div>
</body>
</html>