Prev | Next

TOC | Index

J2EETM Developer's Guide
Advanced Topics


Connecting to a URL in an Enterprise Bean

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 enterprise bean. (The sample code is in the doc/guides/ejb/examples/urlconnect directory.)

The getContents method of the HTMLReaderEJB class returns a String that contains the contents of an HTML file. This method 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:

TABLE 9-10 Resource References Dialog for the HTMLReaderEJB Example

Dialog Field

Value

Coded Name url/MyURL
Type java.net.URL
Authentication Container
URL http://<host>:8000/index.html

TABLE 9-11 JNDI Names for the HTMLReaderEJB Example

Component/Reference
Name

JNDI Name

HTMLReaderBean MyHTMLPReader
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 installation. To change this URL, go to the Resource Refs tabbed pane for the enterprise bean, select the entry in the table, and edit the URL field.

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

1. Exit the Application Deployment Tool.

2. Stop the J2EE server.

3. In the bin/j2ee script, add the following options to the PROPS environment variable:

-Dhttp.proxyPort=<port> -Dhttp.proxyHost=<host>
The <port> is the proxy's port number and <host> is the name of your proxy host.

4. In the lib/security/Server.policy file, edit the following line:

permission java.net.SocketPermission "*:0-65535", "connect";
Modify the line so that it appears as follows::

permission java.net.SocketPermission "*", "connect";
5. Start the J2EE server.

6. Start the Application Deployment Tool.



Prev | Next

TOC | Index


Copyright © 2000 Sun Microsystems, Inc. All rights reserved.