Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Sending HTML Form, Reading Returned XML data, Special Character Problem

Thread view: 
quentinmacdougall@gmail.com - 27 Oct 2005 18:03 GMT
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?
Roedy Green - 27 Oct 2005 20:03 GMT
>java.net.URL("https://localhost/servlet/SearchUser");

IIRC your problem is that when you do this in the browser, some
browsers handle the https for you, but that is not sufficient for a
standalone Java program. With Java 1.4.1+ SSL is builtin via JSSE Java
Secure Socket Extension. See the javax.net.ssl class.

Check out http://mindprod.com/jgloss/ssl.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.