<html>
	<head>
		<title>JSP Data Entry Form </title>
	</head>
	<body bgcolor="lightyellow">
    	<h2 align='center'>The Ice Cream Shop - JSP</h2>

		<%@ page import="java.util.Arrays" %>
		<% 
			String   firstname = "";
			String   lastname  = "";
			String   address   = "";
			String[] flavors   = null;								//select option list
			String[] toppings  = null;								//checkboxes
			String   cc        = "";
			String   allFlav   = "";
			String   allTop    = "";
        		String   error     = "";
        		String   message   = "";

			String method = request.getMethod();
																	//if GET  --> first time
        		if (method.equals("POST"))								//if POST --> client submitted form
			{														//this must be subsequent time
				firstname  = request.getParameter("fname");
				lastname   = request.getParameter("lname");			//get entered data
				address    = request.getParameter("addr");
				flavors    = request.getParameterValues("flavor"); 
				toppings   = request.getParameterValues("topping"); 
				cc         = request.getParameter("creditCard");

				allFlav = Arrays.toString(flavors);					//join the array [x, y, z]
				allTop  = Arrays.toString(toppings);

				if (cc == null) cc = "";							//change null to ""						

				if (cc.equals(""))
					error  = "Please enter credit card";				
				if (toppings == null)
					error  = "Please enter ice cream topping(s)";				
				if (flavors  == null)
					error  = "Please enter ice cream flavor(s)";				
   				if (address.equals(""))
					error  = "Please enter address";
				if (lastname.equals(""))
					error  = "Please enter last name";
				if (firstname.equals(""))
					error  = "Please enter first name";

				if (error.equals(""))
					message = "<b>Your ice cream order will be processed </b><br/>"
					  		+ "<b>For: </b>" + firstname + " " + lastname + "<br/>"
					  		+ "<b>At.: </b>" + address   + "<br/>"
					  		+ allFlav + " with " + allTop + "<br/>"
					  		+ "Using credit card: " + cc  + "<br/>";
			}			        
		%>

<form name="form" method="POST" action="example4.jsp">

<fieldset style='width:625px; height:280px; border-color:gold'>
<legend>Enter Fields Below</legend>

<table bgcolor=tan width=615px>
<tr>
	<td><b>Enter First Name
	<td><input type="text" name="fname" value="<%=firstname%>" />
	    <b>  Last Name
	    <input type="text" name="lname" value="<%=lastname%>" />
<tr>
	<td><b>Enter Address
	<td><textarea name="addr" rows="4" cols="59"><%=address%></textarea>
<tr>
	<td><b>Ice Cream Flavor
	<td><select name="flavor" SIZE="4" multiple="multiple">
    		<option <%= allFlav.indexOf("Vanilla")       >=0 ?"selected" :"" %> >Vanilla </option>
    		<option <%= allFlav.indexOf("Chocolate")     >=0 ?"selected" :"" %> >Chocolate</option>
    		<option <%= allFlav.indexOf("Strawberry")    >=0 ?"selected" :"" %> >Strawberry</option>
    		<option <%= allFlav.indexOf("Butter Pecan")  >=0 ?"selected" :"" %> >Butter Pecan</option>
    		<option <%= allFlav.indexOf("Rocky Road")    >=0 ?"selected" :"" %> >Rocky Road</option>
    		<option <%= allFlav.indexOf("French Vanilla")>=0 ?"selected" :"" %> >French Vanilla</option>
    		<option <%= allFlav.indexOf("Pistachio")     >=0 ?"selected" :"" %> >Pistachio</option>
	</select>
<tr>
	<td><b>Select Topping
	<td>
	<input type="checkbox" name="topping" value="HF" <%= allTop.indexOf("HF")>=0 ?"checked" :"" %>/> Hot Fudge
	<input type="checkbox" name="topping" value="S"  <%= allTop.indexOf("S") >=0 ?"checked" :"" %>/> Sprinkles
	<input type="checkbox" name="topping" value="N"  <%= allTop.indexOf("N") >=0 ?"checked" :"" %>/> Nuts
	<input type="checkbox" name="topping" value="WC" <%= allTop.indexOf("WC")>=0 ?"checked" :"" %>/> Whipped Cream
<tr>
	<td><b>Credit Card
	<td>
	<input type="radio" name="creditCard" value="MC"   <%= cc.equals("MC")   ?"checked" :"" %>/> Master Card
	<input type="radio" name="creditCard" value="VISA" <%= cc.equals("VISA") ?"checked" :"" %>/> Visa
	<input type="radio" name="creditCard" value="AMEX" <%= cc.equals("AMEX") ?"checked" :"" %>/> American Express
	<input type="radio" name="creditCard" value="DISC" <%= cc.equals("DISC") ?"checked" :"" %>/> Discover
<tr>
	<td colspan=2>
	<input type="submit" value="  Place Order  " />
	<input type="reset"  value="Cancel"          />
</table>
</fieldset>
</form>         
<p style="color:red"><%=error%></p>
<p><%=message%></p>
</body>
</html>