URLs - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

URLs

Description:

URL class is the simplest way for Java to locate & retrieve data ... Otherwise it is trickier!! Accessing password-protected sites. Through HTTP authentication ... – PowerPoint PPT presentation

Number of Views:300
Avg rating:3.0/5.0
Slides: 16
Provided by: v5o5jotqkg
Category:
Tags: trickier | urls

less

Transcript and Presenter's Notes

Title: URLs


1
URLs URIs
  • Lecture 5 (chapter 7)

2
URL class
  • URL class is the simplest way for Java to locate
    retrieve data from a network
  • Though Java can handle only few protocols
    content types, later you will learn how to write
    install new content protocol handlers
  • Java.net.URL class is an abstraction of a URL
    such as http//www.unb.ca
  • It extends java.lang.Object and it is a final
    class that cannot be subclassed
  • Public final class URL extends Object implements
    Serializable

3
URL class cont.
  • Though storing URL as a string would be easy, it
    is helpful to think URL as object with fields
    that include the protocol, hostname, port, path,
    query string, and fragment identifier (ref), each
    of which may be set independently.
  • EX http//v5o5jotqkgfu3btr91t7w5fhzedjaoaz8igl.un
    bsj.ca/jlight/

4
Creating new URLs
  • We can construct instances of java.net.URL
  • There are SIX constructors differing in the
    information they require

5
Constructing a URL from a string
  • Public URL(String url) throws MalformedURLExceptio
    n
  • Following code constructs a URL object from a
    string
  • try
  • URL unew URL(http//www.unb.ca)
  • catch (MalformedURLException ex)
  • System.err.println(ex)
  • Example to find out which protocols a virtual
    machine supports. It tries to construct a URL
    object for each 14 protocols ( 8 std 3 custom
    made) for various java APIs 4 undocumented
    protocols used internally by HotJava. If the
    constructor succeeds, then the protocol is
    supported Otherwise it throws MalformedURLExcepti
    on error. Protocol Tester Example

6
Constructing a URL from its component parts
  • This constructor builds a URL from THREE strings
    specifying the protocol, the hostname and the
    file
  • Public URL(String protocol, String hostname,
    String file) throws MalformedURLException
  • The constructor sets the port to -1, so that the
    default port for the protocol will be used
  • try
  • URL unew URL(http, http//www.unb.ca,
    /decker.html)
  • catch (MalformedURLException ex)
  • System.err.println(ex)

7
Constructing relative URL
  • Public URL(URL base, String relative) throws
    MalformedURLException
  • try
  • URL u1 new URL(http, http//www.unb.ca,
    /prospectivestudents.html)
  • URL u2 new URL(u1,studentslist.html)
  • catch (MalformedURLException ex)
  • System.err.println(ex)

8
Splitting a URL into pieces
  • URLs are composed of five pieces
  • The protocol (scheme)
  • The authority
  • The path
  • The fragment identifier (section/ref)
  • The query string
  • Read only access to these parts of a URL is
    provided by FIVE public methods
  • getFile() getHost() getProtocol() getPort()
    and getRef()
  • Java 1.3 adds four more methods getQuery()
    getPath() getUserInfo() and getAuthority()

9
Retrieving data from a URL
  • Public InputStream openStream() throws
    IOException
  • Public URLConnection openConnection() throws
    IOException
  • Public URLConnection openConnection(Proxy proxy)
    throws IOException //1.5
  • Public Object getContent() throws IOException
  • Public Object getContent(Class classes) throws
    IOException //1.3

10
URLEncoder and URLDecoder classes
  • To solve difference in dealing with Oses
  • Encoding Any character that is not ASCII is
    converted into byte and each byte is written as
    sign followed by two hexadecimal digits.
  • Ex
  • spaces are encoded as 20
  • Public static String encode (String s)
  • Public static String decode (String s)

11
URI Class
  • Includes not only URLs but also URNs
  • URI class is purely about identification of
    resources and parsing of URIs
  • Most standards XML are defined interms of URIs
    //Java 1.4 after

12
Constructing a URI
  • URL depends on an underlying protocol whereas URI
    does not.
  • URIs are built from strings.
  • Ex of some constructors for URIs
  • Public URI(String uri) throws URISyntaxException
  • Public URI(String scheme, String
    schemeSpecificPart, String fragment) throws
    URISyntaxException
  • Public URI(String scheme, String host, String
    path, String fragment) throws URISyntaxException
  • Public URI(String scheme, String host, String
    path, String query, String fragment) throws
    URISyntaxException
  • Ex
  • URI Lect5 http, www.unbsj.ca,
    jlight.html, v5o5jotqkgfu3btr91t7w5fhzedjaoaz8
    igl, Lect5

13
Communicating the server-side programs through Get
  • URL class makes it easier for Java applets
    applications to communicate server-side programs
    such as CGIs, servlets, PHP pages, and others
    using GET method
  • If you have written the server side program you
    know what form to ask
  • Otherwise it is trickier!!

14
Accessing password-protected sites
  • Through HTTP authentication
  • Through cookies HTML forms (not a good soln)
  • The authenticator class included in the Java.net
    package, use username password to authenticate
  • Public PasswordAuthentication(String userName,
    char password)
  • Each accessed using getter method
  • Public String getUserName()
  • Public char getPassword90

15
Book examples
  • http//www.cafeaulait.org/books/jnp3/examples/07/
Write a Comment
User Comments (0)
About PowerShow.com