Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
440
Chapter 17 Using Applets As Servlet Front Ends
2. Create a
URLConnection
object. The
openConnection
method of
URL
returns a
URLConnection
object. This object
will be used to obtain streams with which to communicate.
URLConnection connection = dataURL.openConnection();
3. Instruct the browser not to cache the URL data. The first
thing you do with the
URLConnection
object is to specify that
the browser not cache it. This guarantees that you get a fresh
result each time.
connection.setUseCaches(false);
4. Set any desired HTTP headers. If you want to set HTTP
request headers (see Chapter 4), you can use
setRequest
Property
to do so.
connection.setRequestProperty("header", "value");
5. Create an input stream. There are a variety of appropriate
streams, but a common one is
BufferedReader
. It is at the
point where you create the input stream that the connection to
the Web server is actually established behind the scenes.
BufferedReader in =
new BufferedReader(new InputStreamReader(
connection.getInputStream()));
6. Read each line of the document. The HTTP specification
stipulates that the server closes the connection when it is done.
When the connection is closed,
readLine
returns
null
. So,
simply read until you get
null
.
String line;
while ((line = in.readLine()) != null) {
doSomethingWith(line);
}
7. Close the input stream.
in.close();
Home page for this book: www.coreservlets.com; Home page for sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved