Download
FAQ History |
API
Search Feedback |
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 theHTMLReaderBean
class returns aString
that contains the contents of an HTML file. This method looks up thejava.net.URL
object associated with a coded name (url/MyURL
), opens a connection to it, and then reads its contents from anInputStream
. Here is the source code for thegetContents
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 providedHTMLReaderApp
application, the mapping has already been specified. The next section shows you how to verify the mapping indeploytool
.Deploying the Application
- In
deploytool
, open theHTMLReaderApp.ear
file, which resides in this directory:
<
INSTALL
>/j2eetutorial14/examples/ejb/provided-ears/
- Verify the resource reference.
- Verify the mapping of the reference to the JNDI name.
- Deploy the
HTMLReaderApp
application.- In the Deploy Module dialog box, do the following:
Running the Client
To run the
HTMLReaderClient
program, do the following:
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.