Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes How to programatically submit a html form? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to programatically submit a html form?" Watch "How to programatically submit a html form?" New topic
Author

How to programatically submit a html form?

Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi,

I have an HTML form. I have to write a java program wherein after getting some specific condition, I have to submit the HTML form without opening it in IE. How can I do this? Can someone show some light on this?

Thanks and Regards,
Narendranath


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Choopong Songcharoenkij
Greenhorn

Joined: Sep 28, 2004
Posts: 18
if u don't want to open the HTML file in any browsers to submit ur form,
you can programatically sending a POST request using either a Socket or URL. The URL's example is look like:

try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

// Send data
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}
--------------------------------------
Using SOCKET:

try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

// Create a socket to the host
String hostname = "hostname.com";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);

// Send header
String path = "/servlet/SomeServlet";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("POST "+path+" HTTP/1.0\r\n");
wr.write("Content-Length: "+data.length()+"\r\n");
wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
wr.write("\r\n");

// Send data
wr.write(data);
wr.flush();

// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}

u can find the additional information at http://javaalmanac.com
hope it helpful.


SCEA5.0 (In Progress), SCBCD5.0, SCBCD1.3, SCWCD5.0, SCWCD1.4, SCJD, SCJP6.0, SCJP5.0, SCJP1.4, SCJA1.0<br />OMG Certified UML 2.0 Professional - Fundamental - Intermediate - Advanced <br />Oracle Database 10g Administrator Certified Associate
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Thanks a lot. I'll try this. But which one is more optimistic?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You can probably save a lot of work by using http://jakarta.apache.org/commons/httpclient/


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: How to programatically submit a html form?