INEW 2338, Advanced Java Study Guide:  Network Programming

Published November 3, 2004
By Richard G. Baldwin

File: Inew2338Sg002.htm


Welcome

The purpose of this series of tutorial lessons is to help you learn the essential topics covered in INEW 2338, Advanced Java.

These lessons provide questions, answers, and explanations designed to help you to understand the essential Java features covered by the Advanced Java course.

The textbook for this course is Advanced Java Internet Applications, Second Edition by Art Gittleman, ISBN 1-57676-096-0.  This study guide is for Chapter 2 in the textbook.  There is no study guide for Chapter 1.

In addition to the textbook, I recommend that you study my extensive collection of online Java tutorial lessons.  Those tutorial lessons are published at http://www.dickbaldwin.com.

For this particular study guide, you should study lessons 550 through 568 at the URL given above.


Questions

1.  True or False?  A URL has four parts.  If true, list the four parts.

Answer and Explanation

2.  True or False?  The following is a valid URL for Professor Baldwin's page at Austin Community College.

http://www.austincc.edu:80/baldwin/index.html

Answer and Explanation

3.  True or False?  The following (abbreviated) URL can be used to connect to Professor Baldwin's page at Austin Community College.

http://www.austincc.edu/baldwin/index.html

Answer and Explanation

4.  True or False?  The following (abbreviated) URL can be used to connect to Professor Baldwin's page at Austin Community College.

http://www.austincc.edu/baldwin/

Answer and Explanation

5.  True or False?  The following (abbreviated) URL can be used to connect to Professor Baldwin's page at Austin Community College.

http://www.austincc.edu/

Answer and Explanation

6.  What output is produced by the following program?

/*File Inew2338_001.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_001{
  public static void main(String[] args){
    BufferedReader dataIn;
    String theURL =
               "http://www.austincc.edu/baldwin";
    try{
      URL url = new URL(theURL);
      dataIn = new BufferedReader(
        new InputStreamReader(url.openStream()));
      String lineData;
      while((lineData =
                     dataIn.readLine()) != null){
        System.out.println(lineData);
      }//end while
      dataIn.close();
    }catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_001

Answer and Explanation

7.  Assume that the root directory on your computer contains a file named junk.txt.  What output is produced by the following program?

/*File Inew2338_002.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_002{
  public static void main(String[] args){
    BufferedReader dataIn;
    String theURL = "file://localhost/junk.txt";
    try{
      URL url = new URL(theURL);
      dataIn = new BufferedReader(
        new InputStreamReader(url.openStream()));
      String lineData;
      while((lineData =
                     dataIn.readLine()) != null){
        System.out.println(lineData);
      }//end while
      dataIn.close();
    }catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_002

Answer and Explanation

8.  Assume that the root directory on your computer contains a file named junk.txt.  What output is produced by the following program?

/*File Inew2338_004.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_004{
  public static void main(String[] args){
    BufferedReader dataIn;
    String theURL = "file://junk.txt";
    try{
      URL url = new URL(theURL);
      dataIn = new BufferedReader(
        new InputStreamReader(url.openStream()));
      String lineData;
      while((lineData =
                     dataIn.readLine()) != null){
        System.out.println(lineData);
      }//end while
      dataIn.close();
    }catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_004

Answer and Explanation

9.  True or False?  The following program named Inew2338_006 behaves the same as the program named Inew2338_001 discussed earlier.
 
/*File Inew2338_006.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_006{
  public static void main(String[] args){
    try{
      URL theUrl = new URL(
              "http://www.austincc.edu/baldwin");
      URLConnection connection =
                         theUrl.openConnection();
      InputStream inputStream =
                     connection.getInputStream();
      InputStreamReader inputStreamReader =
              new InputStreamReader(inputStream);
      BufferedReader dataIn =
           new BufferedReader(inputStreamReader);

      String lineData;
      while((lineData =
                     dataIn.readLine()) != null){
        System.out.println(lineData);
      }//end while
      dataIn.close();
    }catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_006

Answer and Explanation

10.  Which output is most likely to be produced by the following program?

/*File Inew2338_008.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_008{
  public static void main(String[] args){
    try{
      URL theUrl = new URL("http://www.austincc."
                     + "edu/baldwin/index.html");
      URLConnection connection =
                         theUrl.openConnection();
      String key = "Status";
      String data = "";
      int cnt = 0;
      data = connection.getHeaderField(cnt);
      while(data != null){
        System.out.println(key + ": " + data);
        key =
             connection.getHeaderFieldKey(++cnt);
        data = connection.getHeaderField(cnt);
      }//end while loop
    }catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_008

Answer and Explanation

11.  Assume that the IP address for your computer is assigned by DHCP (dynamically assigned) behind your company firewall, and that you run the following program.  The program is designed to get and display an IP address.  Which of the following is the most likely IP address displayed by the program?

/*File Inew2338_010.java
Copyright 2004, R.G.Baldwin

Tested using SDK 1.4.2 and WinXP
************************************************/

import java.net.*;

class Inew2338_010{
  public static void main(String[] args){
    try{
      System.out.println(
                     InetAddress.getLocalHost());
    }catch(UnknownHostException ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_010

Answer and Explanation

12.  What output is produced by the following program?

*File Inew2338_012.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_012{
  public static void main(String[] args){
    String server = "www.austincc.edu";
    int port = 80;
    try{
      ServerSocket socket =
                   new ServerSocket(server,port);
      BufferedReader inputStream =
                   new BufferedReader(
                     new InputStreamReader(
                       socket.getInputStream()));
      PrintWriter outputStream =
             new PrintWriter(
               new OutputStreamWriter(
                 socket.getOutputStream()),true);
      outputStream.println("GET /baldwin/");
      String line = null;
      while((line = inputStream.readLine())
                                        != null){
        System.out.println(line);
      }//end while loop
      socket.close();
    }//end try
    catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_012

Answer and Explanation

13.  Assume that your computer is configured such that a server running locally on your computer is known by the name localhost.

Assume that you compile and run the following program in a process window at the command prompt and that the following text appears in the process window signaling that the program is running:
Waiting for call

Assume that the folder that contains the source code for the program also contains a file named index.html.

Assume that you enter the following URL into the address window of your HTTP browser and press the Enter key, directing the browser to connect to the server named localhost.

http://localhost/

Which of the following statements are true and which are false?

A.  The following text will be displayed in the process window containing the command prompt:

Received a call
Waiting for call
Running Connection thread
Socket closed

B.  The contents of the java source code file named Inew2338_14.java will be displayed by the browser.

C.  Each time you refresh the browser the following text will be displayed again in the process window containing the command prompt:

Received a call
Waiting for call
Running Connection thread
Socket closed

/*File Inew2338_014.java
Copyright 2004, R.G.Baldwin

Tested using SDK 1.4.2 and WinXP
************************************************/

import java.net.*;
import java.io.*;
import java.util.*;

public class Inew2338_014{
  public static void main(String[] argv){
    SimpleServer serverThread =
                              new SimpleServer();
  }//end main
}//end class Inew2338_014
//=============================================//

class SimpleServer extends Thread{
  SimpleServer(){//constructor
    start();
  }//end constructor
  //-------------------------------------------//

  public void run(){
    try{
      ServerSocket serverSocket =
                          new ServerSocket(8000);
      while(true){
        System.out.println("Waiting for call");
        new Connection(serverSocket.accept());
      }//end while loop
    }catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end run
}//end class SimpleServer
//=============================================//

class Connection extends Thread{
  Socket socket;
    DataOutputStream byteOutput = null;

  Connection(Socket socket){//constructor
    System.out.println("Received a call");
    this.socket = socket;
    setPriority( NORM_PRIORITY-1 );
    start();
  }//end constructor
  //-------------------------------------------//

  public void run(){
    System.out.println(
                    "Running Connection thread");

    try{
      byteOutput = new DataOutputStream(
                       socket.getOutputStream());
      String fileToSend = "Inew2338_014.java";
      FileInputStream fileInputStream =
                 new FileInputStream(fileToSend);
      byte[] data =
           new byte[fileInputStream.available()];
      fileInputStream.read(data);
      byteOutput.write(data);
      byteOutput.flush();
      socket.close();
      System.out.println("Socket closed");
    }//end
    catch(Exception ex){
      ex.printStackTrace();
    }//end catch

  }//end run method
}//end class Connection
//=============================================//

Answer and Explanation

14.  This problem involves two separate programs named Inew2338_016 and Inew2338_018 as shown below.

Assume that your computer is configured such that a server running locally on your computer is known by the name localhost.

Assume that you compile and run the program named Inew2338_016 in a process window at the command prompt and that the following text appears in the process window signaling that the program is running:
Waiting for call

Assume that the folder that contains the source code for the program named Inew2338_016 also contains a file named index.html.

Assume that you then compile and run the program named Inew2338_018 in a separate process window.

What output is produced by the program named Inew2338_018?

/*File Inew2338_016.java
Copyright 2004, R.G.Baldwin

Tested using SDK 1.4.2 and WinXP
************************************************/

import java.net.*;
import java.io.*;
import java.util.*;

public class Inew2338_016{
  public static void main(String[] argv){
    SimpleServer serverThread =
                              new SimpleServer();
  }//end main
}//end class Inew2338_016
//=============================================//

class SimpleServer extends Thread{
  SimpleServer(){//constructor
    start();
  }//end constructor
  //-------------------------------------------//

  public void run(){
    try{
      ServerSocket serverSocket =
                          new ServerSocket(8000);
      while(true){
        System.out.println("Waiting for call");
        new Connection(serverSocket.accept());
      }//end while loop
    }catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end run
}//end class SimpleServer
//=============================================//

class Connection extends Thread{
  Socket socket;
    DataOutputStream byteOutput = null;

  Connection(Socket socket){//constructor
    System.out.println("Received a call");
    this.socket = socket;
    setPriority( NORM_PRIORITY-1 );
    start();
  }//end constructor
  //-------------------------------------------//

  public void run(){
    System.out.println(
                    "Running Connection thread");

    try{
      byteOutput = new DataOutputStream(
                       socket.getOutputStream());
      String fileToSend = "index.html";
      FileInputStream fileInputStream =
                 new FileInputStream(fileToSend);
      byte[] data =
           new byte[fileInputStream.available()];
      fileInputStream.read(data);
      byteOutput.write(data);
      byteOutput.flush();
      socket.close();
      System.out.println("Socket closed");
    }//end
    catch(Exception ex){
      ex.printStackTrace();
    }//end catch

  }//end run method
}//end class Connection
//=============================================//

 
/*File Inew2338_018.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_018{
  public static void main(String[] args){
    String server = "localhost";
    int port = 8000;
    try{
      Socket socket = new Socket(server,port);
      BufferedReader inputStream =
                   new BufferedReader(
                     new InputStreamReader(
                       socket.getInputStream()));
      String line = null;
      while((line = inputStream.readLine())
                                        != null){
        System.out.println(line);
      }//end while loop
      socket.close();
    }//end try
    catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_018

Answer and Explanation

15.  This problem involves five Java source code files and one Windows batch file.  If you are running under Windows, you can run the batch file to automate the compilation and execution of the programs.  If you are not running under Windows, you can manually perform each of the operations described by the remarks in the batch file in the manner that is appropriate for your operating system.

What output is produced by the following system of programs?

echo off

rem This batch file is used to drive a system of 
rem program files that illustrate the use of 
rem stubs, skeletons, and remote objects in a 
rem system of distributed objects.

rem Delete existing class files for neatness.
del Inew2338_020*.class

rem Compile all source code files in a single 
rem process window.
javac Inew2338_020Client.java
javac Inew2338_020Skel.java

rem Start skeleton running in a different process
rem window.
start java Inew2338_020Skel

rem Pause and allow skeleton to become ready. 
rem Press any key to continue
pause

rem Start client running is original process
rem window
java Inew2338_020Client

rem Pause and save original window so that output 
rem can be seen. Press any key to terminate.
pause

 
/*File Inew2338_020Skel.java
Tested using SDK 1.4.2 under WinXP
************************************************/
import java.io.*;
import java.net.*;

public class Inew2338_020Skel extends Thread{
  Inew2338_020Server myServer;

  //Constructor
  public Inew2338_020Skel(
                      Inew2338_020Server server){
    //Save a reference to the remote object
    this.myServer = server;
  }
  public void run(){
    try{
    //Create a server socket and block waiting
    // for a connection request
      ServerSocket serverSocket =
                          new ServerSocket(2000);
      Socket socket = serverSocket.accept();
      //If the returned socket is valid, process
      // the connection.
      while(socket != null){
        //Get input and output streams on the
        // socket
        ObjectInputStream inStream =
                      new ObjectInputStream(
                        socket.getInputStream());
        ObjectOutputStream outStream =
                     new ObjectOutputStream(
                       socket.getOutputStream());

        //Read incoming request. Determine the
        // type of request and satisfy it.  Only
        // two types of requests are allowed and
        // there is no code to deal with bad
        // requests.
        String method =
                   (String)inStream.readObject();

        if(method.equals("getA")){
          //Invoke method on remote object
          int a = myServer.getA();
          //Send results back to stub client
          outStream.writeInt(a);
          outStream.flush();
        }else if(method.equals("getB")){
          String b = myServer.getB();
          outStream.writeObject(b);
          outStream.flush();
        }//end else
      }//end while loop
    }catch(Throwable t) {
      t.printStackTrace();System.exit(0); }
  }
  //===========================================//
  public static void main(String args[]){
    //Instantiate the remote object
    Inew2338_020Server remoteObj =
               new Inew2338_020Server("red", 28);
    //Instantiate the skeleton, linking it to the
    // remote object and start the skeleton
    // running
    Inew2338_020Skel skel =
                 new Inew2338_020Skel(remoteObj);
    skel.start();
  }//end main()
}//end class Inew2338_020Skel

 
/*File Inew2338_020Server.java
Tested using SDK 1.4.2 under WinXP
************************************************/
public class Inew2338_020Server
                         implements Inew2338_020{
  int a;
  String b;

  //Constructor
  public Inew2338_020Server(String b, int a){
    this.a = a;
    this.b = b;
  }//end constructor
  //-------------------------------------------//

  public int getA(){
    return a;
  }//end getA()
  //-------------------------------------------//

  public String getB(){
    return b;
  }//end getB()
}//end class Inew2338_020Server

 
/*File Inew2338_020Client.java
Tested using SDK 1.4.2 and WinXP
************************************************/
public class Inew2338_020Client{
  public static void main(String[] args){
    try{
      Inew2338_020 stub = new Inew2338_020Stub();
      System.out.print(stub.getB() + " ");
      System.out.println(stub.getA());
    }catch(Throwable t) {t.printStackTrace();}
  }//end main()
}//end class Inew2338_020Client

 
/*File Inew2338_020.java
Tested using SDK 1.4.2 under WinXP
************************************************/
public interface Inew2338_020{

  public int getA() throws Throwable;
  public String getB() throws Throwable;
}//end Inew2338_020

 
/*File Inew2338_020Stub.java
Tested using SDK 1.4.2 under WinXP
************************************************/
import java.io.*;
import java.net.Socket;

//Note that this stub class implements the same
// interface that is implemented by the class of
// the remote object.
public class Inew2338_020Stub
                         implements Inew2338_020{
  Socket socket;
  ObjectOutputStream outStream;
  ObjectInputStream inStream;

  public Inew2338_020Stub()throws Throwable{
    //Instantiate a network connection to the
    // skeleton.
    socket = new Socket("localhost",2000);
  }//end constructor
  //-------------------------------------------//

  //A very important concept is illustrated here.
  // The following two methods are
  // implementations of the interface that this
  // class implements.  However, these methods
  // serve as local proxies for the actual
  // methods on the remote object that the client
  // desires to invoke.
  public int getA( ) throws Throwable{
    //Get input and output streams on the socket
    outStream = new ObjectOutputStream(
                       socket.getOutputStream());
    inStream = new ObjectInputStream(
                        socket.getInputStream());
    // Send a String to the skeleton indicating
    // the method that is to be invoked on the
    // remote object.  Then get and return the
    // value returned by the skeleton.
    outStream.writeObject("getA");
    outStream.flush();
    return inStream.readInt();
  }//end getA()
  //-------------------------------------------//

  public String getB()throws Throwable{
    //Get input and output streams on the socket
    outStream = new ObjectOutputStream(
                       socket.getOutputStream());
    inStream = new ObjectInputStream(
                        socket.getInputStream());
    outStream.writeObject("getB");
    outStream.flush();
    return (String)inStream.readObject();
  }//end getB()
}//end class Inew2338_020Stub

Answer and Explanation



Copyright 2004, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com


Answers and Explanations


Answer 15

B.  red 28

Explanation 15

This set of simple programs illustrates the use of stubs, skeletons, and remote objects in a system of distributed objects.  This is the basis for the more complex topics of Remote Method Invocation (RMI) and Common Object Request Broker Architecture (CORBA).

You can learn more about these topic in lessons 568, 600, and 620 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 15


Answer 14

C.  The text content of the file named index.html.

Explanation 14

The program named Inew2338_016 is a simple server program that listens to port 8000.  When a connection request is received on port 8000, the program sends the contents of the file named index.html to the computer that made the connection request.

The program named Inew2338_018 is a simple client program that makes a connection request on port 8000 to a server running as localhost.  When the connection request is honored, the program reads and displays the material sent by the server in response to the connection request.

You can learn more about this topic in lessons 550 through 562   at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 14


Answer 13

All three statements are false.

Explanation 13

My Netscape browser responds, "The connection was refused when attempting to contact localhost."

My Internet Explorer browser responds, "The page cannot be displayed."

However, changing the URL entered into the browser's address window to the following causes all three statements to be true.

http://localhost:8000/

This is because the server is listening for connection requests on port 8000 while the browser attempts to connect to port 80 by default.  When the browser is instructed to connect to port 8000, the behavior of the system is as described by the three statements.

You can learn more about this topic in lesson 550 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 13


Answer 12

A.  Compiler or Runtime Error

Explanation 12

The ServerSocket class is not the correct class to use for an HTTP client program.   Furthermore, the ServerSocket class doesn't have a constructor that matches the one that is called for in the program.  Therefore, the program won't compile.

The following corrected version of the program, which replaces ServerSocket with Socket, produces an output that matches the following answer:

B.  The text contents of the file named index.html at http://www/austincc.edu/baldwin.

/*File Inew2338_012.java
Tested using SDK 1.4.2 and WinXP
************************************************/
import java.net.*;
import java.io.*;

class Inew2338_012{
  public static void main(String[] args){
    String server = "www.austincc.edu";
    int port = 80;
    try{
      Socket socket = new Socket(server,port);
      BufferedReader inputStream =
                   new BufferedReader(
                     new InputStreamReader(
                       socket.getInputStream()));
      PrintWriter outputStream =
             new PrintWriter(
               new OutputStreamWriter(
                 socket.getOutputStream()),true);
      outputStream.println("GET /baldwin/");
      String line = null;
      while((line = inputStream.readLine())
                                        != null){
        System.out.println(line);
      }//end while loop
      socket.close();
    }//end try
    catch(Exception ex){
      ex.printStackTrace();
    }//end catch
  }//end main
}//end class Inew2338_012

You can learn more about this topic in lesson 560 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 12


Answer 11

B. 10.44.1.117

Explanation 11

Addresses assigned by DHCP are (or should be) non-routable addresses for private networks.  The list of non-routable IP addresses for private networks includes the following addresses:

10.0.0.1 - 10.255.255.254
172.16.0.1 - 172.31.255.254
192.168.0.1 - 192.168.255.254

See further discussion of non-routable IP addresses for private networks at http://support.easystreet.com/easydsl/general-info/iptutorial.html.

 As of September 13, 2004, the IP address in answer A (216.239.39.147) is the public IP address for www.google.com.  You should be able to enter that IP address into your Netscape or Internet Explorer browser's address window and use it to connect to Google.

You can learn more about this topic in lesson 552 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 11


Answer 10

B.
Status: HTTP/1.1 200 OK
Date: Wed, 08 Sep 2004 20:31:49 GMT
Server: Apache/1.3.22 (Unix) PHP/4.1.2 mod_perl/1.26
Last-Modified: Sat, 21 Aug 2004 18:03:09 GMT
ETag: "3a6342-e82-41278e5d"
Accept-Ranges: bytes
Content-Length: 3714
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html

Explanation 10

The output listed under B is the set of response headers actually sent by the server and displayed by the program.

The material listed under C is more similar to, but not necessarily identical to, the request headers sent  by the program to the server.  The most obvious element in the material under C that identifies that material as request headers is the existence of the GET command in the first line.  (Note, however, that there are other commands that can be sent to an HTTP server.  See the HTTP specification at http://www.w3.org/Protocols/.)

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 10


Answer 9

True

Explanation 9

Although the program named Inew2338_006 contains several changes relative to the program named Inew2338_001, most of the changes are insignificant.  The significant change is the replacement of the openStream method in Inew2338_001 by the code shown below:
 
      URLConnection connection =
                         theUrl.openConnection();
      InputStream inputStream =
                     connection.getInputStream();

The openStream method of the URL class is a convenience replacement method for openConnection().getInputStream().

The openConnection method is a method of the URL class that returns a reference to an object of type URLConnection.  According to Sun, the getInputStream method of the URLConnection class "Returns an input stream that reads from this open connection."

While an object of the URL class implicitly uses an object of the URLConnection class, explicit usage of an object of the URLConnection class provides capabilities that are not provided by the use of an object of the URL class.

You can learn more about this topic in lessons 554 and 556 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 9


Answer 8

A.  Compiler or Runtime Error

Explanation 8

This program produces the following runtime error:

 java.net.UnknownHostException: junk.txt

However, if you were to replace the URL in the program with the following URL, the output would be a listing on the screen of the file named junk.txt:

file:///junk.txt

Replacing localhost with a single "/" character, (which really means leaving the host name empty and keeping the slash character that normally follows the host name), causes the TCP/IP network access system to default to localhost.

Back to Question 8


Answer 7

B.  A listing on the screen of the file named junk.txt - usually, but not always.

Explanation 7

Typically, replacing the protocol name and host address in the URL with file://localhost will cause the TCP/IP network access system to default to the local machine and cause it to access the specified file.  Note, however that at least on a Windows machine, there is a configuration file named hosts, which can be modified to cause the system to consider a name other than localhost to represent the local machine.

You can learn more about this topic in lesson 552 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 7


Answer 6

C.  None of the above.

Explanation 6

This program will connect to the website specified by the given abbreviated URL and will download the file named index.html.  However, it will display that file as plain text and will not render it in the manner that you would see if you opened the URL in your web browser.

A very important aspect of this program is the invocation of the openStream method on the URL object, which, according to Sun, "Opens a connection to this URL and returns an InputStream for reading from that connection."

You can learn more about this topic in lesson 556 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 6


Answer 5

False

Explanation 5

Although this abbreviated URL will connect to ACC's web site, it won't connect to Professor Baldwin's page on that web site.  It doesn't contain any information that would point to a resource on Professor Baldwin's page.

You can learn more about this topic in lesson 550 at http://www.dickbaldwin.com/tocadv.htm.

Back to Question 5  


Answer 4

True

Explanation 4

According to Gittleman, "index.html is the default file name."  Therefore, it can be omitted from the path to the resource.  Note that this abbreviated URL contains a partial path to the resource.

You can learn more about this topic in lesson 550 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 4


Answer 3

True

Explanation 3

Although the port isn't shown in the URL, the default port number of 80 is used by the browser to connect to the server using the HTTP protocol.

You can learn more about this topic in lesson 550 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 3


Answer 2

True

Explanation 2

The URL contains the four required parts and those parts point to Professor Baldwin's page at Austin Community College.

You can learn more about this topic in lesson 550 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 2  


Answer 1

True

Explanation 1

According to Gittleman, a URL has four parts:

You can learn more about this topic in lessons 550 and 556 at http://www.dickbaldwin.com/tocadv.htm.

You might also want go to Google and search for the following keywords:

This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier.  Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.

Back to Question 1



Copyright 2004, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-