Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

HttpURLConnection onSubmit problem.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I want to do: I want to post data in a form, and get the results back.( new html page ). Normally I can do this without problems. But if the <form> tag contains onSubmit action, I no longer get the next page when I get the OutputStream, I get the current page again. How do I get around this problem and get the next page?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian
Sorry, but I don't understand what problem you have and how you are doing things?
Please explain in more details. I didn't understand how the form's onSubmit plays a role in what you are doing? Because if we are submitting a form by Http URL connection then 'form' tag doesn't play a role I guess...
Regards
Maulin
 
Brian Stelzer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay here is a snippet from the actual source( html )
function validate_existingcustomer() {
var ret = true;
if (window.document.existingcustomer.j_password.value == "") {
alert("j_password is empty.");
ret = false;
}
if (window.document.existingcustomer.j_username.value == "") {
alert("j_username is empty.");
ret = false;
}
return ret;
}
</script>
<form name="existingcustomer" action="j_signon_check" method="POST" onSubmit="return validate_existingcustomer();">
<table cellpadding="5" cellspacing="0" border="0">
<tr>
<td class="petstore" align="center" colspan="2">
<b>Yes.</b>
</td>
</tr>
<tr>
<td class="petstore_form" align="right">
<b>User Name:</b>
</td>
<td class="petstore_form">



<input size="15" type="text" name="j_username" value="j2ee" class="petstore_form">
</td>
</tr>
<tr>
<td class="petstore_form" align="right">
<b>Password:</b>
</td>
<td class="petstore_form">
<input size="15" type="password" name="j_password" value="j2ee" class="petstore_form">
</td>
</tr>


<tr>
<td align="center" colspan="2">
<input class="petstore_form" name="submit" type="submit" value="Sign In">
</td>
</tr>
<tr>
<td align="center" colspan="2">
Remember My User Name
<input type="checkbox" name="j_remember_username">
</td>
</tr>
</table>
</form>
Normally I can do the following in my java code and get the next page, but when there is the onSubmit attribute( like above ) it returns the current page, not the next page.
HttpURLConnection hUC = (HttpURLConnection)uRL.openConnection();
hUC.setRequestMethod("POST");
hUC.setDoOutput(true);
hUC.setDoInput(true);
// Send over any params and close the connection. Only if POST connection.
PrintWriter out = new PrintWriter(hUC.getOutputStream());
out.print(_propertyString);
out.close();
// Read in file contents into vector, vector by nature will keep order.
BufferedReader reader = new BufferedReader(new InputStreamReader(hUC.getInputStream()));
String line = reader.readLine();
while (line != null) {
vCFileContents.add(line);
line = reader.readLine();
}

I've looked online about this problem, and the only thing I've found was my question, by others, but no answers.
 
Brian Stelzer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay here is a snippet from the actual source( html )
function validate_existingcustomer() {
var ret = true;
if (window.document.existingcustomer.j_password.value == "") {
alert("j_password is empty.");
ret = false;
}
if (window.document.existingcustomer.j_username.value == "") {
alert("j_username is empty.");
ret = false;
}
return ret;
}
</script>
<form name="existingcustomer" action="j_signon_check" method="POST" onSubmit="return validate_existingcustomer();">
<table cellpadding="5" cellspacing="0" border="0">
<tr>
<td class="petstore" align="center" colspan="2">
<b>Yes.</b>
</td>
</tr>
<tr>
<td class="petstore_form" align="right">
<b>User Name:</b>
</td>
<td class="petstore_form">



<input size="15" type="text" name="j_username" value="j2ee" class="petstore_form">
</td>
</tr>
<tr>
<td class="petstore_form" align="right">
<b>Password:</b>
</td>
<td class="petstore_form">
<input size="15" type="password" name="j_password" value="j2ee" class="petstore_form">
</td>
</tr>


<tr>
<td align="center" colspan="2">
<input class="petstore_form" name="submit" type="submit" value="Sign In">
</td>
</tr>
<tr>
<td align="center" colspan="2">
Remember My User Name
<input type="checkbox" name="j_remember_username">
</td>
</tr>
</table>
</form>
Normally I can do the following in my java code and get the next page, but when there is the onSubmit attribute( like above ) it returns the current page, not the next page.
HttpURLConnection hUC = (HttpURLConnection)uRL.openConnection();
hUC.setRequestMethod("POST");
hUC.setDoOutput(true);
hUC.setDoInput(true);
// Send over any params and close the connection. Only if POST connection.
PrintWriter out = new PrintWriter(hUC.getOutputStream());
out.print(_propertyString);
out.close();
// Read in file contents into vector, vector by nature will keep order.
BufferedReader reader = new BufferedReader(new InputStreamReader(hUC.getInputStream()));
String line = reader.readLine();
while (line != null) {
vCFileContents.add(line);
line = reader.readLine();
}

I've looked online about this problem, and the only thing I've found was my question, by others, but no answers.
 
Brian Stelzer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
_propertyString is
?j_password=j2ee&j_username=j2ee&submit=Sign+In
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian
So, are you trying to make URLConnection to the html page you have shown here? Still I am unclear. The reason I am unclear is - I am trying to think what you are trying to achieve here? To me it would be something this in usual case,
1. I have a servlet/jsp to which I am doing a POST via html
2. Now, I want to directly do posting via a java program to that servlet/jsp instead of that HTML page I have
3. So, I create url connection to that servlet/jsp and post parameters as you do here by writing it to a output stream.
Now, what you show here is the HTML and as you note in my second point above we make connection to the destination servlet/jsp rather than the HTML. So, I would assume that we should be trying to establish URL Connection to the "j_signon_check" servlet. If we are doing that then why that onSubmit question would arise as we don't have to worry about that HTML at all...
Please bare with me here...
Regards
Maulin
 
Your mother is a hamster and your father smells of tiny ads!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic