URL Connections

A uniform resource locator (URL) specifies the location of a resource on the web. The HTMLReaderBean class shows how to connect to a URL from within an enterprise bean.


Note: The source code for this example is in this directory: <INSTALL>/j2eetutorial14/ejb/htmlreader/src/.


The getContents method of the HTMLReaderBean class returns a String that contains the contents of an HTML file. This method looks up the java.net.URL object associated with a coded name (url/MyURL), opens a connection to it, and then reads its contents from an InputStream. Here is the source code for the getContents method.

public StringBuffer getContents() throws HTTPResponseException 
{

   Context context;
   URL url;
   StringBuffer buffer;
   String line;
   int responseCode;
   HttpURLConnection connection;
   InputStream input;
   BufferedReader dataInput;
 
   try {
      context = new InitialContext();
      url = (URL)context.lookup("java:comp/env/url/MyURL");  
      connection = (HttpURLConnection)url.openConnection();
      responseCode = connection.getResponseCode();
   } catch (Exception ex) {
       throw new EJBException(ex.getMessage());
   }

   if (responseCode != HttpURLConnection.HTTP_OK) {
      throw new HTTPResponseException("HTTP response code: " + 
         String.valueOf(responseCode));
   }

   try {
      buffer = new StringBuffer();
      input = connection.getInputStream();
      dataInput =
          new BufferedReader(new InputStreamReader(input));
      while ((line = dataInput.readLine()) != null) {
         buffer.append(line);
         buffer.append('\n');
      }  
   } catch (Exception ex) {
       throw new EJBException(ex.getMessage());
   }
   return buffer;
} 

Running the HTMLReaderBean Example

The coded name (url/MyURL) must be mapped to a JNDI name (a URL string). In the provided HTMLReaderApp application, the mapping has already been specified. The next section shows you how to verify the mapping in deploytool.

Deploying the Application

  1. In deploytool, open the HTMLReaderApp.ear file, which resides in this directory:
  2. <INSTALL>/j2eetutorial14/examples/ejb/provided-ears/

  3. Verify the resource reference.
    1. In the tree, expand the HTMLReaderApp node.
    2. Select the HTMLReaderBean node.
    3. Select the Resource Ref's tab.
    4. Note the URL resource reference for url/MyURL.
  4. Verify the mapping of the reference to the JNDI name.
    1. In the tree, select the HTMLReaderApp node.
    2. Click the Sun-specific Settings button.
    3. Note the mapping of url/MyURL (coded in HTMLReaderBean.java) to this URL:
    4.   http://localhost:8080/index.html

  5. Deploy the HTMLReaderApp application.
  6. In the Deploy Module dialog box, do the following:
    1. Select the Return Client JAR checkbox.
    2. In the field below the check box, enter the following:
    3. <INSTALL>/j2eetutorial14/examples/ejb/htmlreader

Running the Client

To run the HTMLReaderClient program, do the following:

  1. In a terminal window, go to this directory:
  2. <INSTALL>/j2eetutorial14/examples/ejb/htmlreader/

  3. Type the following command on a single line:
  4. appclient -client HTMLReaderAppClient.jar

  5. The client should display the source of the HTML file at this URL:
  6. http://localhost:8080/index.html