Java Reference
In-Depth Information
Today, you learned how to use URLs, URL connections, and input streams in combina-
tion to pull data from the Web into your program.
Networking can be extremely useful. The
WebReader
project is a rudimentary web
browser—it can load a web page or RSS file into a Java program and display it, although
it doesn't do anything to make sense of the markup tags, presenting the raw text deliv-
ered by a web server.
You created a socket application that implements the basics of the finger protocol, a
method for retrieving user information on the Internet.
You also learned how client and server programs are written in Java using the nonblock-
ing techniques in the
java.nio
package.
To use nonblocking techniques, you learned about the fundamental classes of Java's new
networking package: buffers, character encoders and decoders, socket channels, and
selectors.
17
Q How can I do
POST
form submissions?
A
You can mimic what a browser does to send forms using
POST
. Create a
URL
object
for the form-submission address, such as http://www.example.com/cgi/mail2.cgi,
and then call this object's
openConnection()
method to create a
URLConnection
object. Call the connection's
setDoOutput()
method to indicate that you will be
sending data to this URL and then send the connection a series of name-value pairs
that hold the data, separated by ampersand characters (
“&”
).
For instance, if the
mail2.cgi
form is a CGI program that sends mail with
name
,
subject
,
email
, and
comments fields
, and you have created a
PrintWriter
stream called
pw
connected to this CGI program, you can post information to it
using the following statement:
pw.print(“name=YourName&subject=Book&email=you@yourdomain.com&”
+ “comments= A+POST+example “””);