** Start of listing of h4int-java.cgi ** ** End of listing of h4int-java.cgi ** Here is the directory entry for that file: ^ Note user execute permission needed for all CGI scripts. ** Start of listing of H4int.java ** import java.util.Properties; import java.util.Enumeration; import java.util.HashMap; import java.util.StringTokenizer; import java.net.URLDecoder; class H4int { /* ** This first part deals with CGI details, skip to next '**' comment */ private static HashMap hmform; /* Decoded key=val pairs from HTML FORM */ public static void emitHtmlPlain() { System.out.println("Content-type: text/plain;charset=us-ascii"); System.out.println(""); } public static void showAllSystemProperties() { Properties pr = System.getProperties(); Enumeration en = pr.propertyNames(); while (en.hasMoreElements()) { Object oKey = en.nextElement(); String sKey = (String)oKey; String sVal = pr.getProperty(sKey); System.out.println("Key[" + sKey + "] -> [" + sVal + "]"); } } public static String getEncodedFormContents(boolean verbose) { Properties pr = System.getProperties(); String rm = pr.getProperty("cgi.request_method"); if (verbose) System.out.println("** Request method is " + rm); String efc; // Encoded Form Contents, comes from different places below String qs = pr.getProperty("cgi.query_string"); if ("GET".equals(rm)) { //System.out.println("Good! I can do GET."); if (verbose) System.out.println("** EncodedFormContents = Query string = " + qs); return(qs); } else if ("POST".equals(rm)) { if (!(qs.equals(""))) System.out.println("*** Ignoring query string: " + qs); String sCL = pr.getProperty("cgi.content_length"); if (verbose) System.out.println("Content length (string) = " + sCL); int nCL = Integer.parseInt(sCL); if (verbose) System.out.println("Content length (int) = " + nCL); byte[] buf = new byte[nCL]; try { int nRead = System.in.read(buf, 0, nCL); if (verbose) System.out.println("Number of bytes actually read = " + nRead); } catch (Exception ex) { System.out.println("Got an exception trying to read POST " + "data: " + ex); return(""); } efc = new String(buf); if (verbose) System.out.println("Encoded form contents (from " + "std.input) = " + efc); return(efc); } else { System.out.println("Type of service neither GET nor POST"); return(""); } } public static HashMap decodeFormContents(String efc, boolean verbose) { HashMap hm = new HashMap(); StringTokenizer st = new StringTokenizer(efc, "&"); while (st.hasMoreTokens()) { String pair = st.nextToken(); if (verbose) System.out.println("Pair is [" + pair + "]"); StringTokenizer st2 = new StringTokenizer(pair, "=", true); int ntok = st2.countTokens(); //System.out.println("#tokens=" + ntok); if (!((ntok>=2) && (ntok<=3))) { System.out.println("#tokens = " + ntok + " is out of range."); return(hm); } try { String rawkey = st2.nextToken(); //System.out.println("RawKey=[" + rawkey + "]"); String key = URLDecoder.decode(rawkey); //System.out.println("Key=[" + key + "]"); String del = st2.nextToken(); //System.out.println("Del=[" + del + "]"); if (!("=".equals(del))) { System.out.println("Bug: Delimiter as token not self?"); return(hm); } String rawval; if (st2.hasMoreTokens()) rawval = st2.nextToken(); else rawval = ""; // Bugfix 2007.Feb.10 for empty form field //System.out.println("RawVal=[" + rawval + "]"); String val = URLDecoder.decode(rawval); //System.out.println("Val=[" + val + "]"); if (verbose) System.out.println("Key=[" + key + "] val=[" + val + "]"); hm.put(key,val); } catch (Exception ex) { System.out.println("While trying to decode one key=val pair [" + pair + "], got exception: " + ex); return(hm); } if (st2.hasMoreTokens()) { System.out.println("*** Bug: More tokens remain."); return(hm); } } return(hm); } /* ** End of CGI library stuff, now extensions to java API needed for h4int, skip to next '**' comment */ public static int stringIndexOfNot(String str, char notch, int ix0, int ixzp1) { int ixstr; for (ixstr=ix0; ixstr=0) return("After good number, garbage character '" + str.charAt(ixgar2) + "'."); return(""); } public static void stringCheckDecimalStringTalk(String str) { System.out.print("** Testing [" + str + "]..."); String res = stringCheckDecimalString(str); if ("".equals(res)) { System.out.println("Good!"); String trimstr = str.trim(); if (trimstr.charAt(0)=='+') trimstr=trimstr.substring(1); try { int n1 = Integer.parseInt(trimstr); System.out.println("Counting: " + n1 + " " + (n1+1) + " " + (n1+2) + " " + (n1+3)); } catch (Exception ex) { System.out.println("parseInt threw an exception (" + ex.getClass() + "): " + ex.getMessage()); } } else System.out.println("Bad: " + res); } /* This is how I tested the above before interfacing to CGI. */ public static void testRig() { System.out.println("Hello test..."); stringCheckDecimalStringTalk(""); stringCheckDecimalStringTalk(" "); stringCheckDecimalStringTalk("x +42"); stringCheckDecimalStringTalk(" x+42"); stringCheckDecimalStringTalk(" + 42x"); stringCheckDecimalStringTalk(" +q42 "); stringCheckDecimalStringTalk(" +42x"); stringCheckDecimalStringTalk(" -42 x"); stringCheckDecimalStringTalk("42 "); stringCheckDecimalStringTalk("-42 "); stringCheckDecimalStringTalk(" 42 "); stringCheckDecimalStringTalk(" +42 "); System.out.println("End of test."); } public static void keyGetFieldTell(String key) { String sval = (String)(hmform.get(key)); System.out.println("Contents of [" + key + "] are [" + sval + "]"); stringCheckDecimalStringTalk(sval); } public static void newStuff() { keyGetFieldTell("num1"); keyGetFieldTell("num2"); keyGetFieldTell("num3"); keyGetFieldTell("num4"); keyGetFieldTell("num5"); keyGetFieldTell("num6"); keyGetFieldTell("num7"); keyGetFieldTell("num8"); } public static void main(String args[]) { emitHtmlPlain(); //showAllSystemProperties(); String efc = getEncodedFormContents(false); System.out.println("* Encoded form contents are: \"" + efc + "\""); hmform = decodeFormContents(efc, false); newStuff(); //testRig(); } } ** End of listing of H4int.java ** Here is the directory entry for that source file and its compilation: -rw------- 1 rem user 9012 Feb 28 2007 H4int.java -rw------- 1 rem user 6472 Feb 28 2007 H4int.class ^ Note user execute permission *not* needed for separate program files.