*
The moose likes I/O and Streams and the fly likes How to POST data to a form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply locked New topic
Author

How to POST data to a form

Huw Morgan
Greenhorn

Joined: Mar 07, 2006
Posts: 23
I hope this is the right place, it is basically about use of URL and OutputStreamWriter but is has aspects of other things.

I have a test web page that has 1 button, and will change the value of a text label when the button is pressed. It is developed with Java Studio Creator. This works when I use a browser.

I am trying to write a java applet to interact with the web server. I just want it to press the button. I use the code below [1] (from javaalmanac.com). It receives the page OK (see the output of the program [2]), but the button is not pressed (the code should change from not pressed to pressed).

Any help greatly apperitiated.

[1]

[2] data = form1%3Abutton1=Submit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en-GB" lang="en-GB"><head><meta http-equiv="Cache-Control" conte
nt="no-cache"/><meta http-equiv="Pragma" content="no-cache"/><title>test2 Title<
/title><link type="text/css" rel="stylesheet" href="resources/stylesheet.css"/><
/head><body style="-rave-layout: grid"><form id="form1" method="post" action="/s
msserver/faces/test2.jsp;jsessionid=77015A9ABE16DB8C7BFFC7E7A730F370" enctype="a
pplication/x-www-form-urlencoded">
<span id="form1 utputText1" style="left: 168px; top: 72px; position: absolute">
not pressed</span><input id="form1:button1" type="submit" name="form1:button1" v
alue="Submit" style="left: 168px; top: 144px; position: absolute" /><input type=
"hidden" name="form1" value="form1" /></form></body></html>

[ August 28, 2006: Message edited by: Huw Morgan ]

[ August 28, 2006: Message edited by: Huw Morgan ]
[ August 28, 2006: Message edited by: Huw Morgan ]
Huw Morgan
Greenhorn

Joined: Mar 07, 2006
Posts: 23
From answers on the java.sun.com site, I have updated the program a bit, but the behaviour is the same. I have [1] to [2].

[1]

[2]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Huw Morgan:

I am trying to write a java applet to interact with the web server. I just want it to press the button. I use the code below [1] (from javaalmanac.com). It receives the page OK (see the output of the program [2]), but the button is not pressed (the code should change from not pressed to pressed).


I am confused. What is changing from "not pressed" to "pressed"?
Your HTML form has two fields, form1 and the submit button. The values that you send in the POST request are what the server will receive.
You show us the faulty output in [2] in your first post, but you don't show us what you are expecting. From what I'm reading, the code and server side appear to be working correctly.


[How To Ask Questions On JavaRanch]
Huw Morgan
Greenhorn

Joined: Mar 07, 2006
Posts: 23
I have decided the problem is not my java code, but the data I have passed to the server. I have asked the question at a more web developer forum, see http://www.webdeveloper.com/forum/showthread.php?t=119490 Any input greatly apperitiated, I copy the problem below [2].

I figured this out by using a CGI page that shows what I am passing. The code I have included above works as it should at site [1].

[1] Script at http://www.cgi101.com/book/ch4/post.cgi web form I am copying at http://www.cgi101.com/book/ch4/form.html

[2] What I am trying to do is submit data as a web form to a URL using the POST method (as opposed to the GET method). I have managed to do this to a simple example CGI script [1] using the java code in the above post.

I am trying to get the same code working with my form. I have put my development machine on the web, and you can see the form at 81.187.36.8:18080/smsserver/faces/test2.jsp (I hope). You will notice that if you press the button the text changes (from "not pressed" to "pressed" and then to "no pressed") and if you select something from the drop down box that item is put in the text.

I am trying to replicate this behaviour with my java application rather than the browser. Whatever I put in as values submitted by the POST method I do not get any behaviour from the web server, ie. the text returned always remains as "not pressed".
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Huw Morgan:
Whatever I put in as values submitted by the POST method I do not get any behaviour from the web server, ie. the text returned always remains as "not pressed".


So you are talking about the JSP page you read in response to your POST not having the value "pressed" in it.
What makes the decision to change "not pressed" to "pressed"? I don't see it in any of the code you've given us. Is it in the JSP or a servlet?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Continued here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to POST data to a form