Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending HTML form data using java code

 
Srihari Venkatesan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Srihari Venkatesan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4404
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4404
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic