Q
quentinmacdougall
I am stumped. I have a simple index.html page that contains an HTML
form and sends data to a CGI script. It works as expected.
I tried re-recating this index.html FORM as a small Java program. It
almost works, however, special XML characters are not being encoded
from my Java program where they are encoded when I run my index.html
page.
What am I doing wrong in my java code?
index.html
----------------------------------------------------------------------
<html>
<body>
<FORM NAME="MyTest" ACTION="https://localhost/servlet/SearchUser>
<INPUT TYPE="hidden" NAME="firstname" VALUE="jack+%26+jill">
<INPUT TYPE="hidden" NAME="lastname" VALUE="">
</FORM>
<SCRIPT LANGUAGE="JavaScript" >
document.forms[0].submit()
</SCRIPT>
</body>
</html>
(You see, I encode the ampersand with %26 in the firstname field).
When I run the above index.html file in my browser, the SearchUser CGI
script returns the following XML:
<search>
<request>
<lastname></lastname>
<firstname>Jack+%26+Jill</firstname>
</request>
<total>0</total>
</search>
PostFormTest.java
---------------------------------------------------------
public class FormPostTest
{
public static void
main(String args[]) throws Exception
{
String query = "&firstname=jack+%26+jill&lastname=";
java.net.URL url = new
java.net.URL("https://localhost/servlet/SearchUser");
java.net.HttpURLConnection connection =
(java.net.HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-length",
String.valueOf(query.length()));
//connection.connect();
java.io.DataOutputStream printout = new
java.io.DataOutputStream (connection.getOutputStream());
printout.writeBytes (query);
printout.flush();
printout.close();
java.io.InputStreamReader reader = new
java.io.InputStreamReader(connection.getInputStream(), "ISO-8859-1");
java.io.BufferedReader in = new java.io.BufferedReader(reader);
StringBuffer dataBuffer = new StringBuffer();
String line;
while((line = in.readLine()) != null)
{
dataBuffer.append(line);
}
System.out.println("returned: " + dataBuffer.toString());
}
}
When I run this code, I get the following output:
returned: <search><request><lastname></lastname><firstname>Jack &
Jill</firstname>
</request><total>0</total></search>
You see, the ampersand is not encoded. I would have expected my Java
code to have the same results as my HTML form code. Am I doing
something wrong, or is the web browser doing something else I am not
aware of?
form and sends data to a CGI script. It works as expected.
I tried re-recating this index.html FORM as a small Java program. It
almost works, however, special XML characters are not being encoded
from my Java program where they are encoded when I run my index.html
page.
What am I doing wrong in my java code?
index.html
----------------------------------------------------------------------
<html>
<body>
<FORM NAME="MyTest" ACTION="https://localhost/servlet/SearchUser>
<INPUT TYPE="hidden" NAME="firstname" VALUE="jack+%26+jill">
<INPUT TYPE="hidden" NAME="lastname" VALUE="">
</FORM>
<SCRIPT LANGUAGE="JavaScript" >
document.forms[0].submit()
</SCRIPT>
</body>
</html>
(You see, I encode the ampersand with %26 in the firstname field).
When I run the above index.html file in my browser, the SearchUser CGI
script returns the following XML:
<search>
<request>
<lastname></lastname>
<firstname>Jack+%26+Jill</firstname>
</request>
<total>0</total>
</search>
PostFormTest.java
---------------------------------------------------------
public class FormPostTest
{
public static void
main(String args[]) throws Exception
{
String query = "&firstname=jack+%26+jill&lastname=";
java.net.URL url = new
java.net.URL("https://localhost/servlet/SearchUser");
java.net.HttpURLConnection connection =
(java.net.HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-length",
String.valueOf(query.length()));
//connection.connect();
java.io.DataOutputStream printout = new
java.io.DataOutputStream (connection.getOutputStream());
printout.writeBytes (query);
printout.flush();
printout.close();
java.io.InputStreamReader reader = new
java.io.InputStreamReader(connection.getInputStream(), "ISO-8859-1");
java.io.BufferedReader in = new java.io.BufferedReader(reader);
StringBuffer dataBuffer = new StringBuffer();
String line;
while((line = in.readLine()) != null)
{
dataBuffer.append(line);
}
System.out.println("returned: " + dataBuffer.toString());
}
}
When I run this code, I get the following output:
returned: <search><request><lastname></lastname><firstname>Jack &
Jill</firstname>
</request><total>0</total></search>
You see, the ampersand is not encoded. I would have expected my Java
code to have the same results as my HTML form code. Am I doing
something wrong, or is the web browser doing something else I am not
aware of?