⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resources6.html

📁 j2eePDF格式的电子书
💻 HTML
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>URL Connections</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="Resources5.html" />    <link rel="Next" href="Resources7.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="Resources5.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Resources7.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp79892"> </a><h2 class="pHeading1">URL Connections</h2><a name="wp79894"> </a><p class="pBody">A Uniform Resource Locator (URL) specifies the location of a resource on the Web. The <code class="cCode">HTMLReaderBean</code> class shows how to connect to a URL from within an enterprise bean. </p><a name="wp79895"> </a><p class="pBody">The source code for this example is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;<code class="cVariable">INSTALL</code>&gt;/j2eetutorial14/ejb/htmlreader/src/<a name="wp80968"> </a></pre></div><a name="wp79896"> </a><p class="pBody">The <code class="cCode">getContents</code> method of the <code class="cCode">HTMLReaderBean</code> class returns a <code class="cCode">String</code> that contains the contents of an HTML file. This method looks up the <code class="cCode">java.net.URL</code> object associated with a coded name (<code class="cCode">url/MyURL</code>), opens a connection to it, and then reads its contents from an <code class="cCode">InputStream</code>. Here is the source code for the <code class="cCode">getContents</code> method.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">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(&quot;java:comp/env/url/MyURL&quot;);        connection = (HttpURLConnection)url.openConnection();      responseCode = connection.getResponseCode();   } catch (Exception ex) {       throw new EJBException(ex.getMessage());   }   if (responseCode != HttpURLConnection.HTTP_OK) {      throw new HTTPResponseException(&quot;HTTP response code: &quot; +          String.valueOf(responseCode));   }   try {      buffer = new StringBuffer();      input = connection.getInputStream();      dataInput =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new BufferedReader(new InputStreamReader(input));      while ((line = dataInput.readLine()) != null) {         buffer.append(line);         buffer.append(&#39;\n');      }     } catch (Exception ex) {       throw new EJBException(ex.getMessage());   }   return buffer;}<a name="wp79898"> </a></pre></div><a name="wp79899"> </a><h3 class="pHeading2">Running the HTMLReaderEJB Example</h3><a name="wp81156"> </a><p class="pBody">The coded name (<code class="cCode">url/MyURL</code>) must be mapped to a JNDI name (a URL string). In the provided <code class="cCode">HTMLReaderApp</code> application, the mapping has already been specified. The next section shows you how to verify the mapping in <code class="cCode">deploytool</code>.</p><a name="wp79900"> </a><h4 class="pHeading3">Deploying the Application</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80921"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, open the <code class="cCode">HTMLReaderApp.ear</code> file, which resides in this directory:</li></div><a name="wp80922"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/provided-ears/</code></p><a name="wp81170"> </a><div class="pSmartList1"><li>Verify the resource reference.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81171"> </a><div class="pSmartList2"><li>In the tree, expand the <code class="cCode">HTMLReaderApp</code> node.</li></div><a name="wp81172"> </a><div class="pSmartList2"><li>Select the <code class="cCode">HTMLReaderEJB</code> node.</li></div><a name="wp81173"> </a><div class="pSmartList2"><li>Select the Resource Refs tab.</li></div><a name="wp81174"> </a><div class="pSmartList2"><li>Note the URL resource reference for<code class="cCode"> url/MyURL</code>.</li></div></ol></div><a name="wp81175"> </a><div class="pSmartList1"><li>Verify the mapping of the reference to the JNDI name.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81176"> </a><div class="pSmartList2"><li>In the tree, select the ConfirmerApp node.</li></div><a name="wp81177"> </a><div class="pSmartList2"><li>Select the JNDI Names tab.</li></div><a name="wp81178"> </a><div class="pSmartList2"><li>Note the mapping of <code class="cCode">url/MyURL</code> (coded in <code class="cCode">HTMLReaderBean.java</code>) to this URL:</li></div><a name="wp80923"> </a><p class="pBodyRelative"><code class="cCode">&nbsp;&nbsp;http://localhost:8080/index.html</code></p></ol></div><a name="wp81168"> </a><div class="pSmartList1"><li>Deploy the <code class="cCode">HTMLReaderApp</code> application. </li></div><a name="wp80924"> </a><div class="pSmartList1"><li>In the Deploy Module dialog box, do the following: </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp80925"> </a><div class="pSmartList2"><li>Select the Return Client JAR checkbox. </li></div><a name="wp80926"> </a><div class="pSmartList2"><li>In the field below the check box, enter the following:</li></div><a name="wp80927"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/htmlreader</code></p></ol></div></ol></div><a name="wp79903"> </a><h4 class="pHeading3">Running the Client</h4><a name="wp80830"> </a><p class="pBody">To run the <code class="cCode">HTMLReaderClient</code> program, do the following:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80831"> </a><div class="pSmartList1"><li>In a terminal window, go to this directory: </li></div><a name="wp80832"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/htmlreader/</code></p><a name="wp80833"> </a><div class="pSmartList1"><li>Type the following command on a single line:</li></div><a name="wp80834"> </a><p class="pBodyRelative"><code class="cCode">appclient -client HTMLReaderAppClient.jar</code></p><a name="wp80849"> </a><div class="pSmartList1"><li>The client should display the source of the HTML file at this URL:</li></div><a name="wp81198"> </a><p class="pBodyRelative"><code class="cCode">http://localhost:8080/index.html</code></p></ol></div>    </blockquote>   <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="Resources5.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Resources7.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font>  </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -