java net tutorial

Home arrow Java Tutorials arrow Web Tier APIs arrow java net tutorial
java net tutorial Print E-mail
Contributed by Howell   
Friday, 16 June 2006
Abstract (java net tutorial)

Java net Provides the classes for implementing networking applications. Using the socket classes, you can communicate with any server on the Internet or implement your own Internet server. A number of classes are provided to make it convenient to use Universal Resource Locators (URLs) to retrieve data on the Internet.

Java URL tutorial

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine. More information on the types of URLs and their formats can be found at:

     http://www.ncsa.uiuc.edu/demoweb/url-primer.html
In general, a URL can be broken into several parts. The previous example of a URL indicates that the protocol to use is http (HyperText Transfer Protocol) and that the information resides on a host machine named www.ncsa.uiuc.edu. The information on that host machine is named /demoweb/url-primer.html. The exact meaning of this name on the host machine is both protocol dependent and host dependent. The information normally resides in a file, but it could be generated on the fly. This component of the URL is called the path component.

A URL can optionally specify a "port", which is the port number to which the TCP connection is made on the remote host machine. If the port is not specified, the default port for the protocol is used instead. For example, the default port for http is 80. An alternative port could be specified as:

     http://www.ncsa.uiuc.edu:8080/demoweb/url-primer.html
 A URL may have appended to it a "fragment", also known as a "ref" or a "reference". The fragment is indicated by the sharp sign character "#" followed by more characters. For example,

     http://java.sun.com/index.html#chapter1
 This fragment is not technically part of the URL. Rather, it indicates that after the specified resource is retrieved, the application is specifically interested in that part of the document that has the tag chapter1 attached to it. The meaning of a tag is resource specific.

An application can also specify a "relative URL", which contains only enough information to reach the resource relative to another URL. Relative URLs are frequently used within HTML pages. For example, if the contents of the URL:

     http://java.sun.com/index.html

An example of Creating a URL(java net url)

 try {
        // With components.
        URL url = new URL("http", "hostname", 80, "index.html");
   
        // With a single string.
        url = new URL("
http://hostname:80/index.html");
    } catch (MalformedURLException e) {
    }


Parsing a URL (java net url)


    try {
        URL url = new URL("
http://hostname:80/index.html#_top_");
   
        String protocol = url.getProtocol();    // http
        String host = url.getHost();            // hostname
        int port = url.getPort();               // 80
        String file = url.getFile();            // index.html
        String ref = url.getRef();              // _top_
    } catch (MalformedURLException e) {
    }


 Sending a POST Request Using a URL  (java net url tutorial)

  try {
        // Construct data
        String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
        data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
   
        // Send data
        URL url = new URL("
http://hostname:80/cgi");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
   
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            // Process line...
        }
        wr.close();
        rd.close();
    } catch (Exception e) {
    }


URL authentication revisited From javaworld
 Summary

The previous tip, Java Tip 46, described how to access a URL that's password-protected from within a Java program, using Java 1.2's new Authenticator class. But what if you and your users are just now moving to Java 1.1? Basically, without Java 1.2, you lose out on the benefits of the Authenticator class. But don't despair! This tip shows you how your Java 1.1 applets and applications can access URLs that require the input of a username and password. (1,250 words)
Many thanks to David Wallace Croft, who tracked down and fixed a bug in the original version of the Base64Converter class. His revised class now appears in the code for this tip.

Full article

An example of Getting an Image from a URL (Java Net URL tutorial)

    try {
        // Create a URL for the image's location
        URL url = new URL("http://hostname:80/image.gif");
   
        // Get the image
        java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }


Related to book

 Java Network Programming, Third Edition

 Buy from  O'Reilly . Get free ebook from Ebooklobby

Book description

Thoroughly revised to cover all the 100+ significant updates to Java Developers Kit (JDK) 1.5, Java Network Programming is a complete introduction to developing network programs (both applets and applications) using Java, covering everything from networking fundamentals to remote method invocation (RMI). It includes chapters on TCP and UDP sockets, multicasting protocol and content handlers, servlets, and the new I/O API. This is the essential resource for any serious Java developer.

 

Last Updated ( Sunday, 23 July 2006 )

  home              contact us

 

©2006-2009 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground