Java Forms
— print (last updated: Oct 12, 2009) print

Select font size:
Download the JavaForms.zip archive. Copy the folder into your NetBeansProjects folder. After some discussion and preparation we will install it as a Web Application with Existing Sources.

NetBeans Commons/Lang Library

This and other Web projects which use and display text input rely on the ability to escape the text input for display with HTML (see below). The standard Java distribution provide no function which does this, but it is provided in the Apache Commons/Lang Java distribution at:
http://commons.apache.org/lang/
You'll need to download and install this package:
commons-lang-2.4-bin.zip
Install as follows:
  1. Unzip the archive, getting the directory commons-lang-2.4. Within it are the files you need:
    commons-lang-2.4.jar
    commons-lang-2.4-sources.jar
    commons-lang-2.4-javadoc.jar
    
  2. Decide on a permanent location for these three files. Possiblities are these:
    on Windows, using
    My Documents\
    as a base
    My Documents\java\commons-lang-2.4.jar
    My Documents\src\commons-lang-2.4-sources.jar
    My Documents\docs\commons-lang-2.4-javadoc.jar
     
    on Linux/Mac, using
    ~/lib
    as a base
    ~/lib/java/commons-lang-2.4.jar
    ~/lib/src/commons-lang-2.4-sources.jar
    ~/lib/docs/commons-lang-2.4-javadoc.jar
  3. In NetBeans, go to Tools Libraries and click the New Library button (bottom, left). In the Library Name, type CommonsLang.
  4. Then add, via the three tabs using the Add ... button:
    1. Classpath tab: navigate to the location of commons-lang-2.4.jar.
    2. Sources tab: navigate to the location of commons-lang-2.4-sources.jar.
    3. Javadoc tab: the Add button indicates a ZIP file, but a JAR file is a ZIP file; navigate to the location of commons-lang-2.4-javadoc.jar
    And we're done.

Installation/Execution of JavaForms in NetBeans

  1. Extract the JavaForms.zip archive as the directory JavaForms in the NetBeans project folder.
  2. Create a new Web Project as Web Application with Existing Source.
  3. In the Location field of the next popup, Browse to JavaForms in the NetBeans project folder. Everything else should probably be as is. Click Next. Click Finish in the next popup.
  4. Right-click on the Libraries entry in the JavaForms project. Select Add Library and choose CommonsLang.
  5. Run the project (the Run button should work).

Web-based applications

Java has the ability to create a dynamic web page by obtaining non-static information, say from a database, and presenting this to the user. The user then interacts with this program-generated web page and further dynamic web pages are created. The user interacts with a web page presenting HTML form elements and/or hyperlinks. After making choices and/or clicking some element (like a button), data is submitted to another program (possibly the same one which generated the original form) which then generates the next web page, and so on.

Web-based applications have a very different style than, say, Java Swing-based GUI applications. They are event-driven, as indicated, but not thought of as a single program, rather a series of programs which start and terminate for each page generated. Even though it's not technically correct, the term stateless applies to web applications due to the fact that they are not a single program running.

The other major difference with web applications is that the activations normally are stacked. This is what the "Back" button in the browser is all about: keeping of a history with the ability to go back to previous activations. This feature has both good points and bad points. Say, for example, a web activation represents a "one-time" transaction, like deleting a database record. The notion of going back to that activation is error-prone at best. One of the features of AJAX-based web applications is the ability to make efficient server-side executions which browser refreshing, and thus avoid stacking. Such a style makes a Web application appear more like other non web-based GUI applications.

Hyperlink activations

The default hyperlink activation is based on HTML code which looks like this:
<a href="some-url">some description</a> 
Hyperlink activations can be made to do other actions by replacing the URL by a JavaScript call:
<a href="javascript:javascript-code">.....</a> 
The above hyperlink's activation is equivalent to this usage with the JavaScript location object:
<a href="javascript:location='some-url'">some description</a> 
There's no point in using this more complicated version, but the following related operation avoids stacking:
<a href="javascript:location.replace('some-url')">some description</a> 
For example, compare the effects of these two hyperlinks:
href="http://www.cs.wcupa.edu/rkline/csc417"
href="javascript:location.replace('http://www.cs.wcupa.edu/rkline/csc417')"

Query data processing

User-generated web data is transmitted as a list of parameters and values in a data string like this:
name1=value1&name2=value2&...
where name1, name2, etc., typically correspond to the names of the form elements. The values are (typically) generated by the user by text entry or selecting from various choices. A parameter name can appear more than once, in which case it is called multi-valued, otherwise, it is single-valued.

GET/POST queries

A GET request to a server-side script is one which sends this parameter data string attached to the script's URL in the form of a query string following a ? after the script name:
http://.../SCRIPT?name1=value1&name2=value2&...
A POST request is one which sends this parameter string to the script through standard input

When to use GET vs. POST

There are reasons for choosing to use GET or POST queries: GET method is the method used by standard hyperlink activations. GET method is the default used by an HTML form (see below). Nevetheless, web-based applications generally prefer POST for form data transmission. Keep in mind that a GET query is "visible" and "repeatable" because the query is stored in the URL. Thus:

Processing parameters

The doGet and doPost methods, which handle browser requests, use these parameters:
(HttpServletRequest request, HttpServletResponse response)
The request parameter is used for input (typically the parameters from the browser request) and response is used for output sent back to the browser.

The javax.servlet.http.HttpServletRequest class inherits most of the functionality you'll use from the javax.servlet.ServletRequest class. The ones I most frequently used are these: If there are no parameters for a given name, the null value is returned. Below is the ShowParams servlet which can be used to see the immediate effects of receiving get inputs through the query string. The example itself contains a description of how to test it by adding query strings.

params.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Parameters</title> </head> <body> <h2>Parameters</h2> In the browser location field, tack on a query string with parameter/value pairs like these: <pre> ?a=1 ?a=1&b=2 ?a=1&b=2&a=3</pre> and view the results below. <hr /> <h4>number of parameters: <%=request.getParameterMap().size()%></h4> <h4>values</h4> <% java.util.Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String param = (String) names.nextElement(); %> parameter: <%=param%><br /> value: <%=request.getParameter(param)%><br /> values: <%=java.util.Arrays.toString(request.getParameterValues(param))%> <br /><br /> <% } %> </body> </html>
This example uses the request.getParameterMap() to obtain a list of all the parameters. In reality, this is rarely needed because we usually have an expectation of which parameter names are going to be used.

The web server environment

The web server environment constitutes all the dynamic information which a server-side program has access to. This includes the parameter information plus other information regarding who is accessing this page with what request, etc. Java makes this information available via the request parameter through a variety of member functions including these: A simple servlet which prints out some of these values is this:

print-environment.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.util.*" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Server Environment</title> <style type="text/css"> body { font-family: monospace; font-size: 10pt; line-height: 3ex; } b { color: red; } </style> </head> <body> <h2>Server Environment</h2> <% java.util.Enumeration names = request.getHeaderNames(); while (names.hasMoreElements()) { String str = (String) names.nextElement(); %> request.getHeader(<%=str%>) => <b><%=request.getHeader(str)%></b><br /> <% } %> request.getMethod() => <b><%=request.getMethod()%></b><br /> request.getPathInfo() => <b><%=request.getPathInfo()%></b><br /> request.getContextPath() => <b><%=request.getContextPath()%></b><br /> request.getQueryString() => <b><%=request.getQueryString()%></b><br /> request.getRemoteUser() => <b><%=request.getRemoteUser()%></b><br /> request.getRequestURI() => <b><%=request.getRequestURI()%></b><br /> request.getRequestURL() => <b><%=request.getRequestURL()%></b><br /> request.getServletPath() => <b><%=request.getServletPath()%></b><br /> request.getRemoteAddr() => <b><%=request.getRemoteAddr()%></b><br /> request.getRemoteHost() => <b><%=request.getRemoteHost()%></b><br /> </body> </html>

HTML Forms

An HTML form is enclosed within the tags:

<form action="PROGRAM" method="METHOD" target="TARGET">

  <!-- form elements -->

</form>
where

Form Components

The HTML form components provide the user interface. They consist of The elements use attributes specific to the element to change the appearance or behavior. Certain attributes, called boolean attributes, have no value and are activated simply be being present. For example, any form element can be disabled by using the boolean attribute
disabled
Using the disabled attribute typically "grays out" the element and makes it unusable, with the content still visible.

JavaScript attributes

These mouse and keyboard JavaScript event attributes take JavaScript code as their values. They can be used to modify the form-handling behavior, for example, by calling the JavaScript "submit()" value:
onclickmouse clicked in element
ondblclick mouse double-clicked in element
onmousedown/onmouseup mouse pressed/released in element
onmouseover mouse over element
onmouseout mouse not over element
onmousemove mouse position changed within element
onkeydown/onkeyupa key was pressed/released
onkeypress a key was pressed, then released
onfocus/onblur element acquires/loses focus by mouse click or tabbing
onselect text selected in a textfield or textarea
onchange value has been modified (for input, select, textarea )

Text Elements, Buttons, Hidden inputs

Text elements different from other form components in that the user determines the value associated with a parameter name. The HTML input element is used to create a variety of different components including textfields, buttons, hidden inputs, checkboxes, radio buttons, etc. The input element has the form:
<input type="TYPE" name="NAME" value="VALUE" ... />
where the NAME identifies the parameter name passed to the action script. These are the TYPE values of interest at this point: The HTML textarea element is used to create mutiline text entry elements. It has the form:
<textarea name="NAME" rows="ROWS" cols="COLS" ... >...</textarea>
where the NAME identifies the parameter name passed to the action script.

These are also used to control behavior ot textfields and textareas:

Displaying User Input

When you want to display user-generated input in a target script, you have to deal with the fact that this input may contain certain HTML-special characters, in particular these: "<", "&", """, ">". There are are possiblities: For most part we want the latter choice, and towards this end we use the Java function from the Commons/Lang package:
org.apache.commons.lang.StringEscapeUtils.escapeHtml(...)
to translate into HTML-special sequences. Insertion into a textfield typically uses code like this:
String escaped_text = StringEscapeUtils.escapeHtml(raw_text);
<input type="text" name="NAME" value="<%= escaped_text %>"/>

Form activation, button handling, hidden inputs

A form need not be activated by the pressing of a button. But if it is, the easiest way to know that the form was activated is to see a non-null value of a named button.

A hidden input name/value combination is one that doesn't show in any explicit way. These often offer a way of maintaining the "state" by passing a value from one form to another.

Sample form and handler

The HTML file text.html is a form with basic text components which also illustrates other features such as buttons and hidden parameters. The handler is the servlet TextHandler.java.

text.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String file = new java.io.File(request.getRequestURI()).getName(); String title = "Text with external handler (" + file + ")"; %> <html> <head> <title><%= title %></title> <link rel="stylesheet" href="css/entry.css" type="text/css" /> <style type="text/css"> table.entry td.left { width: 90px; } </style> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h2><%= title %></h2> <blockquote class="feedback"> Try these "torture" values using both submit buttons:<br /> <tt>tf: a&lt;b&gt;c"d<br /> ta: a&lt;/textarea&gt;b<br /></tt> </blockquote> <form action="TextHandler"> <table class="entry"> <tr> <td class="left">text field (<b>tf</b>):</td> <td><input type="text" class="text" name="tf"/></td> </tr> <tr valign="top"> <td class="left">text area (<b>ta</b>):</td> <td> <!-- no whitespace between start & end tags --> <textarea name="ta" rows="10"></textarea> </td> </tr> </table> <input type="submit" name="raw_button" value="submit (raw)"/> <input type="submit" name="escaped_button" value="submit (escaped)"/> </form> </body> </html>

servlet/TextHandler.java
package servlet; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.commons.lang.StringEscapeUtils; public class TextHandler extends HttpServlet { protected void processRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String tf = request.getParameter("tf"); String ta = request.getParameter("ta"); String raw_button = request.getParameter("raw_button"); String tf_esc = StringEscapeUtils.escapeHtml(tf); String ta_esc = StringEscapeUtils.escapeHtml(ta); String tf_ins; String ta_ins; String title; if (raw_button != null) { title = "Raw input inserted"; tf_ins = tf; ta_ins = ta; } else { title = "Escaped input inserted"; tf_ins = tf_esc; ta_ins = ta_esc; } String file = new java.io.File(request.getRequestURI()).getName(); title += "(" + file + ")"; out.println("<h2>" + title + "</h2>"); out.println("tf: " + tf_ins + "<br />"); out.println("tf value reinserted in a textfield: " + "<input type=\"text\" value=\"" + tf_ins + "\" />" + "<br />"); out.println("ta: " + ta_ins + "<br />"); out.println("ta value reinserted in a textarea: " + "<textarea>" + ta_ins + "</textarea>"); } finally { out.close(); } } // ... }

Selection Elements

Selection elements are those where we are presented with a limited number of choices from which we can choose one or more values. The HTML elements which provide these choices are checkboxes, radio buttons and selection lists. In most situation we have control over the values generated by these elements which stands in contrast to the text elements.

Radio/Checkbox Elements

These types of elements are often used in groups in which the elements have the same name. The effect on checkboxes is to create multi-valued parameter data, whereas with radio buttons the data is single-valued because radio button group selection is mutually exclusive.

The HTML input element is also used to create checkboxes and radio buttons. We use:
<input type="TYPE" name="NAME" value="VALUE" ... /> label
where: The boolean attribute checked causes this element to be pre-selected (a radio group can, of course, have only one pre-selected value). In each case, the value "on" is assigned if there is no value specified.

Selection Lists

The HTML select and option elements are used to create selection lists. The format is like this:
<select name="NAME" ... >
<option value="VALUE">label</option>
...
</select>
Additional attributes for select include these: The option element permits the boolean attribute selected which sets that option to be pre-selected.

Single Selection Elements

From a form handler's point-of-view, parameter which are single-valued can be treated in pretty much the same way. In this respect we identify the two elements as being single-selection elements: A second distinction among these single-selection elements is this: In the former cases, you can never be in the state where nothing is chosen, whereas, in the latter case there is initially no choice made, but once a choice is made you can only go back to "choiceless" state by resetting the form.

In Java, we use request.getParameter(..) to retrieve the value. When no choice is made, this value is null, otherwise it is a non-null String. The following example illustrates some of these possibilities:

single-sel.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String file = new java.io.File(request.getRequestURI()).getName(); String title = "Single Selection with external handler (" + file + ")"; %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><%= title %></title> <link rel="stylesheet" href="css/entry.css" type="text/css" /> <style type="text/css"> table.entry td.left { width: 150px; } </style> </head> <body> <h2><%= title %></h2> <form action="SingleSelHandler"> <table class="entry"> <tbody valign="top"> <tr> <td class="left">radio group no selection (<b>rgn</b>)</td> <td> <input type="radio" name="rgn" value="aa">Choice1 <input type="radio" name="rgn" value="bb">Choice2 <input type="radio" name="rgn" value="cc">Choice3 </td> </tr> <tr> <td class="left">scroll list (<b>scl</b>)</td> <td> <select name="scl" size="3"> <option value="aa">Choice1</option> <option value="bb">Choice2</option> <option value="cc">Choice3</option> </select> </td> </tr> <tr> <td class="left">radio group with selection (<b>rgs</b>)</td> <td> <input type="radio" name="rgs" value="aa" checked >Choice1 <input type="radio" name="rgs" value="bb">Choice2 <input type="radio" name="rgs" value="cc">Choice3 </td> </tr> <tr> <td class="left">drop down list (<b>ddl</b>)</td> <td> <select name="ddl"> <option value="aa">Choice1</option> <option value="bb">Choice2</option> <option value="cc">Choice3</option> </select> </td> </tr> </tbody> </table> <input type="submit" value="Submit" name="button" /> <input type="reset" /> </form> </body> </html>

servlet/SingleSelHandler.java
package servlet; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class SingleSelHandler extends HttpServlet { protected void processRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String rgn = request.getParameter("rgn"); String rgs = request.getParameter("rgs"); String scl = request.getParameter("scl"); String ddl = request.getParameter("ddl"); String button = request.getParameter("button"); String file = new java.io.File(request.getRequestURI()).getName(); String title = "Single Selection Handler (" + file + ")"; out.println("<h2>" + title + "</h2>"); out.println("button: " + button + "</br >"); out.println("rgn: " + rgn + "<br />"); out.println("scl: " + scl + "<br />"); out.println("rgs: " + rgs + "<br />"); out.println("ddl: " + ddl + "<br />"); } finally { out.close(); } } // ... }

Multiple-Selection Elements

Multiple selection element are those which deliver parameters with multiple values. Again, from a form handler's point-of-view, these are treated in pretty much the same way. In this respect we identify the two elements as being multi-selection elements: In these cases, zero or more choices can be selected yielding a parameter whose value is either unset, if nothing is chosen, or multi-valued, if more than one is chosen. Given any preset choices, we can always go back to nothing being chosen.

In Java, we use request.getParameterValues(..) to retrieve the value as an String array. When no choices are made, this value is null.

The following example illustrates some of these possibilities:

multi-sel.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String file = new java.io.File(request.getRequestURI()).getName(); String title = "Multi Selection with external handler (" + file + ")"; %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><%= title %></title> <link rel="stylesheet" href="css/entry.css" type="text/css" /> <style type="text/css"> table.entry td.left { width: 150px; } </style> </head> <body> <h2><%= title %></h2> <form action="MultiSelHandler"> <table class="entry"> <tbody valign="top"> <tr> <td class="left">checkbox group (<b>cbg</b>)</td> <td> <input type="checkbox" name="cbg" value="aa">Choice1 <input type="checkbox" name="cbg" value="bb">Choice2 <input type="checkbox" name="cbg" value="cc">Choice3 </td> </tr> <tr> <td class="left">multi-select scroll list (<b>msc</b>)</td> <td> <select name="msc" size="3" multiple> <option value="aa">Choice1</option> <option value="bb">Choice2</option> <option value="cc">Choice3</option> </select> </td> </tr> </tbody> </table> <input type="submit" value="Submit" name="button" /> </form> </body> </html>

servlet/MultiSelHandler.java
package servlet; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class MultiSelHandler extends HttpServlet { protected void processRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String[] cbg = request.getParameterValues("cbg"); String[] msc = request.getParameterValues("msc"); String button = request.getParameter("button"); String file = new java.io.File(request.getRequestURI()).getName(); String title = "Single Selection Handler (" + file + ")"; out.println("<h2>" + title + "</h2>"); out.println("button: " + button + "</br >"); out.println("cbg: " + java.util.Arrays.toString(cbg) + "<br />"); out.println("msc: " + java.util.Arrays.toString(msc) + "<br />"); } finally { out.close(); } } // ... }
The following example illustrates some of these possibilities:

multi-sel.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String file = new java.io.File(request.getRequestURI()).getName(); String title = "Multi Selection with external handler (" + file + ")"; %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><%= title %></title> <link rel="stylesheet" href="css/entry.css" type="text/css" /> <style type="text/css"> table.entry td.left { width: 150px; } </style> </head> <body> <h2><%= title %></h2> <form action="MultiSelHandler"> <table class="entry"> <tbody valign="top"> <tr> <td class="left">checkbox group (<b>cbg</b>)</td> <td> <input type="checkbox" name="cbg" value="aa">Choice1 <input type="checkbox" name="cbg" value="bb">Choice2 <input type="checkbox" name="cbg" value="cc">Choice3 </td> </tr> <tr> <td class="left">multi-select scroll list (<b>msc</b>)</td> <td> <select name="msc" size="3" multiple> <option value="aa">Choice1</option> <option value="bb">Choice2</option> <option value="cc">Choice3</option> </select> </td> </tr> </tbody> </table> <input type="submit" value="Submit" name="button" /> </form> </body> </html>

servlet/MultiSelHandler.java
package servlet; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class MultiSelHandler extends HttpServlet { protected void processRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String[] cbg = request.getParameterValues("cbg"); String[] msc = request.getParameterValues("msc"); String button = request.getParameter("button"); String file = new java.io.File(request.getRequestURI()).getName(); String title = "Single Selection Handler (" + file + ")"; out.println("<h2>" + title + "</h2>"); out.println("button: " + button + "</br >"); out.println("cbg: " + java.util.Arrays.toString(cbg) + "<br />"); out.println("msc: " + java.util.Arrays.toString(msc) + "<br />"); } finally { out.close(); } } // ... }

Reentrant forms

The term reentrant means that the form call itself as the action handler. The easiest way to create the reentrant effect is to make the action attribute empty:
<form>
or
<form action="" >
Often we want to differentiate between the initial activation of the script (typically with no parameters) and the reentrant activations which do have parameters. We can do so by testing whether a certain parameter (often a button's name) is defined or not.

Reentrant Activation through redirection/forwarding

One can also effect reentrant activation by calling an external script and having the external script "call back" to the original. There are two ways that the external script can do this "call back": Note the obvious difference in that redirection makes the client "do something" by virtue of the response, whereas with forwarding, the server does something by virtue of using the request.

Stable vs. unstable form elements

A static form element, without any programming feature, used in a reentrant script is unstable because it does not preserve any user modifications on a reentrant call. A programmed element which does preserve the input settings on reentry is stable. Creating stability implies that we have to capture the parameter value(s) set by the form, and use these values to reset the form elements accordingly. The method of achieving this effect depends on the element:

Sample Reentrant Scripts

All of the scripts are reentrant. For variety, we've made the text-reentrant.jsp script effect its reentrant action by forwarding through the TextReentrantHandler servlet with the action setting:
action="TextReentrantHandler"
Compare it to the "direct reentrant" behavior by setting
action=""
Also note our first usage of a package import used within a JSP page. This is effected by the special tag:
<%@ page import="org.apache.commons.lang.StringEscapeUtils" %>
One can use multiple import lines like this, or put the imports within the same tag as comma-separated values of the import attribute.

text-reentrant.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="org.apache.commons.lang.StringEscapeUtils" %> <% // The top portion of a script represents the "controller" part. // Here we operation primarily in "Java mode" and capture the // incoming parameters and other server information in order to // use it for various computations and database accesses. String tf = request.getParameter("tf"); String ta = request.getParameter("ta"); String button = request.getParameter("button"); String call = request.getParameter("call"); // The code below, with its HTML emphasis, represents the "view" part // in which we create the user presentation. tf = (tf == null) ? "" : StringEscapeUtils.escapeHtml(tf); ta = (ta == null) ? "" : StringEscapeUtils.escapeHtml(ta); String file = new java.io.File(request.getRequestURI()).getName(); String basename = new java.io.File(request.getHeader("referer")).getName(); String title = "Text form: stable reentrant (" + file + ")"; %> <html> <head> <title><%= title %></title> <link rel="stylesheet" href="css/entry.css" type="text/css" /> <style type="text/css"> table.entry td.left { width: 150px; } </style> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h2><%= title %></h2> <blockquote class="feedback"> <% // this feedback section is primarily used for debugging out.println("referer: " + basename + "<br />"); out.println("button: " + button + "<br />"); out.println("(reentrant) call: " + call + "<br />"); out.println("tf: " + tf + "<br />"); out.println("ta: " + ta + "<br />"); if (call == null) call = "0"; call = Integer.toString(Integer.parseInt(call) + 1); %> </blockquote> <form action="TextReentrantHandler"> <table class="entry"> <tr> <td class="left">text field (<b>tf</b>):</td> <td><input type="text" class="text" name="tf" value="<%= tf %>" /></td> </tr> <tr valign="top"> <td class="left">text area (<b>ta</b>):</td> <td> <!-- no whitespace between start & end textarea tags --> <textarea name="ta" cols="40" rows="10"><%= ta %></textarea> </td> </tr> </table> <input type="submit" name="button" value="submit" /> <input type="hidden" name="call" value="<%= call %>" /> </form> </body> </html>

servlet/TextReentrantHandler.java
package servlet; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class TextReentrantHandler extends HttpServlet { protected void processRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String forwardTo = "text-reentrant.jsp"; request.getRequestDispatcher(forwardTo).forward(request, response); } finally { out.close(); } } // ... }

single-sel-reentrant.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.util.*" %> <% String file = new java.io.File(request.getRequestURI()).getName(); String title = "Single Selection stable reeentrant (" + file + ")"; String button = request.getParameter("button"); String rgn = request.getParameter("rgn"); String ddl = request.getParameter("ddl"); Map<String,String> entries = new LinkedHashMap<String,String>(); entries.put("aa", "Choice1"); entries.put("bb", "Choice2"); entries.put("cc", "Choice3"); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><%= title %></title> <link rel="stylesheet" href="css/entry.css" type="text/css" /> <style type="text/css"> table.entry td.left { width: 150px; } </style> </head> <body> <h2><%= title %></h2> <blockquote class="feedback"> <% out.println("button: " + button + "<br />"); out.println("rgn: " + rgn + "<br />"); out.println("ddl: " + ddl + "<br />"); %> </blockquote> <form> <table class="entry"> <tbody valign="top"> <tr> <td class="left">radio group no selection (<b>rgn</b>)</td> <td> <% for (String value: entries.keySet()) { String label = entries.get(value); String sel = value.equals(rgn) ? "checked" : ""; %> <input type="radio" name="rgn" value="<%= value %>" <%= sel %>><%= label %> <% } %> </td> </tr> <tr> <td class="left">drop down list (<b>ddl</b>)</td> <td> <select name="ddl"> <% for (String value: entries.keySet()) { String label = entries.get(value); String sel = value.equals(ddl) ? "selected" : ""; %> <option value="<%= value %>" <%= sel %>><%= label %></option> <% } %> </select> </td> </tr> </tbody> </table> <input type="submit" value="Submit" name="button" /> </form> </body> </html>

multi-sel-reentrant.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.util.*" %> <% String file = new java.io.File(request.getRequestURI()).getName(); String title = "Multiple Selection stable reentrant (" + file + ")"; String button = request.getParameter("button"); String[] cbg = request.getParameterValues("cbg"); String[] msc = request.getParameterValues("msc"); Map<String,String> entries = new LinkedHashMap<String,String>(); entries.put("aa", "Choice1"); entries.put("bb", "Choice2"); entries.put("cc", "Choice3"); Set<String> cbg_set = new HashSet<String>(); if (cbg != null) for (String s: cbg) cbg_set.add(s); Set<String> msc_set = new HashSet<String>(); if (msc != null) for (String s: msc) msc_set.add(s); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><%= title %></title> <link rel="stylesheet" href="css/entry.css" type="text/css" /> <style type="text/css"> table.entry td.left { width: 150px; } </style> </head> <body> <h2><%= title %></h2> <blockquote class="feedback"> <% out.println("button: " + button + "<br />"); out.println("cbg: " + cbg_set + "<br />"); out.println("msc: " + msc_set + "<br />"); %> </blockquote> <form> <table class="entry"> <tbody valign="top"> <tr> <td class="left">checkbox group (<b>cbg</b>)</td> <td> <% for (String value: entries.keySet()) { String label = entries.get(value); String sel = cbg_set.contains(value) ? "checked" : ""; %> <input type="checkbox" name="cbg" value="<%= value %>" <%= sel %>><%= label %> <% } %> </td> </tr> <tr> <td class="left">multi-select scroll list (<b>msc</b>)</td> <td> <select name="msc" size="3" multiple> <% for (String value: entries.keySet()) { String label = entries.get(value); String sel = msc_set.contains(value) ? "selected" : ""; %> <option value="<%= value %>" <%= sel %>><%= label %></option> <% } %> </select> </td> </tr> </tbody> </table> <input type="submit" value="Submit" name="button" /> </form> </body> </html>


© Robert M. Kline