URL connection, Content handler - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

URL connection, Content handler

Description:

Active connection to a resource specified by a URL. Provides more control over the interaction with a server esp HTTP server than URL class ... – PowerPoint PPT presentation

Number of Views:517
Avg rating:3.0/5.0
Slides: 15
Provided by: v5o5jotqkg
Category:

less

Transcript and Presenter's Notes

Title: URL connection, Content handler


1
URL connection, Content handler Protocol handler
  • Lecture 10
  • Chapters 15, 16, 17

2
URL connection class
  • Active connection to a resource specified by a
    URL
  • Provides more control over the interaction with a
    server esp HTTP server than URL class
  • You can inspect the header sent by server and
    respond accordingly
  • You can set the header fields used in the client
    request
  • You can use URL connection to download binary
    files.
  • Allows you to sent data back to server as PUT or
    POST methods

3
URL connection class.
  • It is part of Javas protocol handler mechanism
    which also includes the URLStreamHandler class

4
Opening URL connections
  • Construct a URL object
  • Invoke the URL objects openConnection() method
    to retrieve a URLConnection object for that URL
  • Configure the URLConnection
  • Read the header fields
  • Get an input stream read data
  • Get an output stream and write data
  • Close the connection

5
To download a webpage with URL connection
  • import java.net.
  • import java.io.
  • public class SourceViewer2 public static void
    main (String args)
  • if (args.length gt 0) try //Open the
    URLConnection for reading
  • URL u new URL(args0)
  • URLConnection uc u.openConnection()
  • InputStream raw uc.getInputStream()
  • InputStream buffer new BufferedInputStream(raw)
    // chain the InputStream to a Reader
  • Reader r new InputStreamReader(buffer)
  • int c
  • while ((c r.read()) ! -1)
  • System.out.print((char) c)
  • catch (MalformedURLException ex)
  • System.err.println(args0 " is not a
    parseable URL")
  • catch (IOException ex) System.err.println(ex)
  • // end if
  • // end main
  • // end SourceViewer2

6
Book examples
  • Look here
  • http//www.cafeaulait.org/books/jnp3/examples/15/

7
What URL connection do that URL class cannot do
  • URLConnection provides access to the HTTP header
  • URLConnection can configure the request
    parameters sent to the server
  • Can read as well as write data to the server

8
Reading the Header info
  • Content-type
  • Content-length
  • Content-encoding
  • Date
  • Last-modified
  • Expires
  • Example http//www.cafeaulait.org/books/jnp3/exam
    ples/15/

9
Writing data to the server
  • Configure the URLConnection
  • Post the data
  • Example of posting the authors name Email ID
    to the website
  • http//www.cafeaulait.org/books/jnp3/examples/15/F
    ormPoster.java

10
Protocol Handlers
  • Designing an architecture requires
  • Handling Protocol
  • Handling content
  • Handling the protocol involves interaction
    between client/server
  • Generating request in the correct format
  • Interpreting the headers that come back with the
    data
  • Acknowledging that data is received etc
  • Content Handlers convert the data into the
    correct format such as InputStream or AudioClip

11
Protocol Handler Implementation
  • FOUR different classes in java.net package work
    together to implement Protocol Handler
  • URL (Concrete class)
  • URLStreamHandler (abstract class)
  • URLConnection (abstract class)
  • URLStreamHandlerFactory (interface)

12
Protocol Handler Implementation
  • The program constructs a URL object
  • The constructor uses the arguments its passed to
    find the protocol part of the URL, such as http
  • The URL() constructor tries to find a
    URLStreamHandler for the given protocol
  • Retrieves from Cache if used before
  • If a URLStreamHandlerFactory has been set, the
    protocol string is passed to the factorys
    createURLStreamHandler() method
  • If not, the constructor attempts to instantiate
    an URLStreamHandler object named protocol.Handler
    in the java or sun protocol packages
  • If all these fails, then throws an exception
  • The program calls the URL objects
    openConnection()
  • URL object asks the URLStreamHandler to return
    the URLConnection object
  • Program uses the methods of the URLConnection
    class to interact with the remote resource

13
Writing a protocol handler
  • Finger protocol URLConnection example in book
  • http//www.cafeaulait.org/books/jnp3/examples/16/

14
Content Handlers
  • Ability to handle various kinds of files such as
    html, gif, jpeg,
  • Browsers come with plug-ins that add the ability
    to view new content types
  • Java team found a way around to only download the
    parts of the program to be updated instead of the
    entire browser!!
  • Unfortunately, java content handlers never really
    made it out of Suns white papers into the
    shipping software!
  • Book Examples
Write a Comment
User Comments (0)
About PowerShow.com