| Author |
sending HTML form data using java code
|
Srihari Venkatesan
Greenhorn
Joined: Jul 15, 2002
Posts: 4
|
|
Greetings friends, I would be grateful if anyone provides me with a solution for below..... I want to send the form values of a HTML web page, to the server where the corresponding script for the HTML is running. How do I do it in java? The 'view source' of the HTML page tells that the form uses 'POST' to send data. How do I proceed...? Thanks in advance. Srihari
|
 |
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
|
|
You need something which can accept and read an HTTP POST. How you do that depends on what software you have on the server. If you have a Java application server of some sort you could write a Servlet or JSP which can receive a POST. If you have Java but no application server you could run your own application server. If you have CGI capability you could write a CGI script in some language to parse the POST data. If you have PHP, ASP, ColdFusion, Zope or whatever, you can use that. Can you tell us a little more about your setup?
|
A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
|
 |
Srihari Venkatesan
Greenhorn
Joined: Jul 15, 2002
Posts: 4
|
|
Hello Frank... I am sorry, I think u misinterpreted my query, I do not want to write code that accepts POST'ed values. What I actually need is a way, to send values(text or checked boxes etc..etc..) of HTML forms(which we generally send it through HTML pages) through java code. The view source of the HTML tells it uses POST method...thanks for prompt response..I will wait for ur next reply... Srihari
|
 |
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
|
|
In which case I'm still baffled, I'm afraid. What exactly do you mean by "through java code"? and where/what do you want to send these values to? If you are already receiving the values in a servlet (for example), you have the form parameters available, what do you want to do with them? Can you give a concrete example, or maybe some code to illustrate what you would like to do ?
|
 |
Srihari Venkatesan
Greenhorn
Joined: Jul 15, 2002
Posts: 4
|
|
Okay...let me make more clear....I think you know name:value pairs..Kindly, let me know if there is a web page that extensively describes with an example how I can Simulate a POST request in Java using URL classes. I got some leads...but not clear, a detailed example would be of big help... Thanks Srihari
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4092
|
|
|
im afraid i dont quite understand either. i tried posting from an applet(Java code) to a servlet once. it worked fine in IE but not in Netscape however. i will try to find my old code. i changed it so many times trying to get it to work with netscape that it may take a while.
|
SCJP
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4092
|
|
ok here are exerpts from one version...it may have unnecessary code from my attempt to make it work in netscape
public void start() { try { URL url = new URL(location); URLConnection con = url.openConnection(); DataInputStream in = new DataInputStream(con.getInputStream()); total = in.readInt(); in.close(); } catch(Exception e) { System.out.println("Cannot open connection to server"); } }
public void quit() { try { URL url = new URL(location); URLConnection con = url.openConnection(); con.setDoOutput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //con.setRequestProperty("Content-Type", "application/octet-stream"); DataOutputStream out = new DataOutputStream(con.getOutputStream()); out.writeInt(total); out.flush(); out.close(); AppletContext ac = getAppletContext(); URL next = new URL("http://javaguy.yi.org/examples/MoreQuiz.html"); ac.showDocument(next, "_self"); } catch(Exception e) { System.out.println("Cannot open connection to server"); } }
|
 |
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
|
|
Ah, you want to send a post requst from your program instead of sending it from a browser form. When I need to do this I use HTTPUnit. It's free, and open source and intended as a Web test tool, but it includes some great, easy to use tools for invoking other web pages. See http://httpunit.sourceforge.net/ for more information, including downloads and tutorials.
|
 |
 |
|
|
subject: sending HTML form data using java code
|
|
|