The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

URL Connections

A Uniform Resource Locator (URL) specifies the location of a resource on the Web. The HTMLReaderEJB class shows how to connect to a URL from within an enterprise bean.

The source code for this example is in the examples/src/ejb/htmlreader directory. To compile the code, go to the examples/src directory and type ant htmlreader.

The getContents method of the HTMLReaderEJB 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. Before deploying the application, you must map the coded name (url/MyURL) to a JNDI name (a URL string). 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;
   DataInputStream 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 DataInputStream(input);
      while ((line = dataInput.readLine()) != null) {
         buffer.append(line);
         buffer.append(`\n');
      }  
   } catch (Exception ex) {
       throw new EJBException(ex.getMessage());
   }

   return buffer;
} 

Tips for Running the HTMLReaderEJB Example

  1. Include the HTTPResponseException class in the enterprise bean.
  2. In the Resource Ref's tab of the bean, specify the values in the following table. Replace the <host> string with the name of the host running the J2EE server.

    Table 31 Resource Ref's for the HTMLReaderEJB Example 
    Dialog Field
    Value
    Coded Name
    url/MyURL
    Type
    java.net.URL
    Authentication
    Container
    URL
    http://<host>:8000/index.html
  3. Use the JNDI names listed in the following table. The JNDI name for the url/MyURL entry should match the URL field of the Resource Ref's tab.

    Table 32 JNDI Names for the HTMLReaderEJB Example 
    Component Type or
    Reference Name
    JNDI Name
    HTMLReaderBean
    MyHTMLReader
    url/MyURL
    http://<host>:8000/index.html

The URL specified in the preceding tables refers to the the default public_html/index.html file of your J2EE SDK installation.

To connect to a URL outside of your firewall, you must perform these tasks:

  1. Exit the deploytool.
  2. Stop the J2EE server.
  3. In the bin/j2ee script, add the following options to the PROPS environment variable. The <port> is the proxy's port number and <host> is the name of your proxy host.
    -Dhttp.proxyPort=<port> -Dhttp.proxyHost=<host> 
    
  4. In the lib/security/Server.policy file, find the following line:
    permission java.net.SocketPermission "*:0-65535", "connect"; 
    

Edit the line so that it appears as follows::

permission java.net.SocketPermission "*", "connect"; 
  1. Start the J2EE server.
  2. Start the deploytool.

Home
TOC
PREV TOC NEXT Search
Feedback