|
Using
the QUERY method (i.e., the calling parameters encoded in
the URL), it is simple to access Servlets or CGI programs from
Java:
URLConnection connect ( new URL("http:\\me\mycgi.sh") ).getConnection();
PrintStream ps = new PrintStream(connect.getOutputStream());
connection.setdoOutput(true)// defaults to false
ps.print(myPostData);
ps.close() // very important to close before reading
InputStream is = new PrintStream(connect.getInputStream());
// read the results
NOTE: URLConnection() is
an abstract class whose sub-class is usually supplied by the environment
(browser). Some browsers allow output without calling "doOutput(true)," which violates the Java
spec and may be confusing when code works in one place and not
the other.
|