| Prev | Next | J2EETM Developer's Guide
Advanced Topics |
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:
HTTPResponseException class in the enterprise bean.<host> string with the name of the host running the J2EE server.)|
Dialog Field
|
Value
|
|---|---|
| Coded Name | url/MyURL |
| Type | java.net.URL |
| Authentication | Container |
| URL | http://<host>:8000/index.html |
url/MyURL entry should match the URL field of the Resource References dialog box.|
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.
3. In the bin/j2ee script, add the following options to the PROPS environment variable:
The-Dhttp.proxyPort=<port> -Dhttp.proxyHost=<host>
<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:
Modify the line so that it appears as follows::permission java.net.SocketPermission "*:0-65535", "connect";
5. Start the J2EE server.permission java.net.SocketPermission "*", "connect";
6. Start the Application Deployment Tool.