private Hashtable parseArguments (String query_string)
{
Hashtable cgi_vars = new Hashtable();
// get the first token scan for the '&' char
StringTokenizer stringToken = new StringTokenizer(query_string,"&");
while (stringToken.hasMoreTokens())
{
index++;
// Split the first token into Variable and Value
StringTokenizer subToken = new StringTokenizer(stringToken.nextToken(),"=");
// Remove the '+' char from the Variable
String variable = plussesToSpaces(subToken.nextToken());
// Remove the '+' char from the Value
String value = plussesToSpaces(subToken.nextToken());
// Create the Keys to store the Variables and the Values in the Hash Table
// the keys will be variable1, value1, variable2, value2, and so forth
String temp1= new String("variable"+index);
String temp2= new String("value"+index);
// Store the variables and the values in the Hash table
cgi_vars.put(temp1,translateEscapes(variable));
cgi_vars.put(temp2,translateEscapes(value));
}
return cgi_vars;
}
private String plussesToSpaces(String query_string)
{
// Substitute the '+' char to a blank char
return query_string.replace('+', ' ');
}
private String translateEscapes(String query_string)
{
int percent_sign = query_string.indexOf('%');
int ascii_val;
String next_escape=null;
String first_part=null;
String second_part=null;
while (percent_sign != -1)
{
next_escape = query_string.substring(percent_sign + 1, percent_sign + 3);
ascii_val = (16 * hexValue(next_escape.charAt(0)) + hexValue(next_escape.charAt(1)));
first_part = query_string.substring(0, percent_sign);
second_part = query_string.substring(percent_sign + 3, query_string.length());
query_string = first_part + (char)ascii_val + second_part;
percent_sign = query_string.indexOf('%', percent_sign + 1);
}
return query_string;
Chapter6. Sample programs (in Java, C, and RPG) 91