Adding components to an EJB Server on Windows 2000
4-9-2003
The "Building an EJB Server" document must be completed before this
document will work.
Create a Java Applet Select src/main/client in the Eclipse Package Explorer. Right click
and choose New Package. Enter a package name of "netdb.client" and
press Finish. Select netdb.client, and right click on New Class.
Enter a name of TestApplet, and enter a superclass of Applet.
Add the following code inside the class:
public void paint(Graphics g)
{
g.drawString("This is an applet", 20, 20);
}
To debug applets, turn on the Java Console from the Java Plug-in in the
control panel.
Build the Java Applet The build.xml file only needs to be changed to add a package or jar.
Adding additional Java classes to an existing jar does not require a change.
Add the following to the build.xml file under the jar target:
Build the main target of the build.xml file. (Double click on "main"
in the Ant window.)
Check that the war in the jboss../default/deploy directory has the
proper contents by running a command prompt and entering: jar -tf
web-client.war. The jar command is in the Sun SDK bin directory. You
should see the netdb-client.jar in the war file.
Refer to the Java Applet Add the following line to the index.jsp file.
<object archive = "netdb-client.jar"
code = "netdb/client/TestApplet.class" width = "400" height = "300" >
A better approach is to use the jsp:plugin tag.
Run JBoss, and click the following link.
http://localhost:8080/web-client.
The text "This is an applet" should display in the rectangle.
Modifying the Java Applet to use an EJB This example requires use of the Java 2.0 platform. For example,
InitialContext requires this platform. You may not want
to use the 2.0 platform if you would like the default browser supported
for most systems. To change Internet Explorer to use 2.0, go to
control panel-Java Plugin-Advanced and set the Java Runtime Environment
to the 1.3.1 or 1.4.1 JRE. Then choose the Browser tab and check the
Microsoft Internet Explorer box.
Replace the paint method of the applet code as follows.
public void paint(Graphics g)
{
try
{
java.util.Properties props = new java.util.Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory" ) ;
props.setProperty("java.naming.provider.url", "localhost:1099") ;
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces" ) ;
TestSessionHome lHome = (TestSessionHome)PortableRemoteObject.narrow(
new InitialContext(props).lookup(TestSessionHome.JNDI_NAME),
TestSessionHome.class);
TestSession lSession = lHome.create();
g.drawString("The id is " + lSession.getNewEntityId(), 10, 40);
}
catch( Throwable e )
{
for(int i=0; i e.getMessage().length())
len = e.getMessage().length()-i;
g.drawString(e.getMessage().substring(i, i+len), 10, 40+((i/60)*20));
}
}
}
It is better to distribute a jndi.properties file, but this code example
shows without. If a jndi.properties file is used, all properties code
should be removed, and can be replaced with:
The name used for the lookup can be found by using lContext.list(""),
and then recursing and finding all names. This can also be seen
in TestSessionBean class source, jndi-name="ejb/test/TestSession".
This example is for an unsigned applet, so edit a .java.policy file
to contain the folowing or run the policytool that comes with the JDK.
Under Windows, the file is under the user's profile directory. Signed
applets do not require changing these permissions, but all jars that
the applet calls must also be signed.
If any access denied errors display, add their IP address to the
.java.policy file.
If the default Java platform (JDK 1.1), is used for Internet Explorer,
then to flush the applet cache in Internet Explorer, hold the Control key,
and press the Refresh button. To reset the cache for Java 2.0, exit and
restart all versions of the Explorer.
The TestApplet currently uses TestSession from the client-test.jar,
so it must be added here.
Change the object element in the index.jsp to:
<object archive = "netdb-client.jar, jnp-client.jar,
jboss-common-client.jar, jbossall-client.jar, log4j.jar, client-test.jar"
code = "netdb/client/TestApplet.class" width = "500"
height = "300" >
Add "test/interfaces/**" to the netdb-client.jar in the build.xml by
changing the includes line to "includes="test/interfaces/**, netdb/client/**""
Add the following line to the build.xml war target. Use "includes=" if
you would like to only put certain jars into the war file. The jars listed
in the object element above are required.
<fileset dir="${jboss.home}/client" />
Once again, the code can be tested by running JBoss, and clicking the following link.
http://localhost:8080/web-client.
The applet should display the entity id one greater than the page, and the
id should increment whenever the applet is redrawn.