Table of Contents
This chapter describes how to create, configure, and execute a JEUS applet program.
An applet is a Java application that is executed in the web browser. To use a Java EE service in an applet, an applet container should be used. Since JEUS does not provide lightweight client containers yet, an applet, that cannot directly access JEUS libraries, should be able to directly use a Java EE service without a container.
An applet enables a user to execute Java applications in the web browser. In addition, an applet can run as JEUS client.
Files required to run an applet exist within the web application. Since the HTML JAVA_CODEBASE is the URL directory containing the code file, JAR files should exist in the same directory where the HTML document of the web application will be deployed to.
This section introduces some sample examples.
An applet should inherit the Applet or JApplet class, and implement the start() method.
The following example shows a client that is performed independently, which means without a client container. Therefore, dependency injection is not used. It looks up the corresponding EJB by using JNDI API.
The same EJB as that of the previous client container is used, and the EJB uses the interface name, 'helloejb.Hello,' as its binding name.
[Example 3.1] <<HelloClient.java>>
package helloejb; import javax.naming.Context; import javax.naming.InitialContext; import java.applet.Applet; import java.util.Hashtable; import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.*; import javax.swing.*; public class HelloClient extends JApplet { public void start() { try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"jeus.jndi.JNSContextFactory"); Context context = new InitialContext(env); Hello hello = (Hello) context.lookup("helloejb.Hello"); System.out.println("EJB output : " + hello.sayHello()); JLabel label = new JLabel(hello.sayHello()); label.setFont(new Font("Helevetica", Font.BOLD, 15)); getContentPane().setLayout(new BorderLayout()); getContentPane().add(label, BorderLayout.CENTER); setSize(500, 250); setVisible(true); } catch (Exception ex) { ex.printStackTrace(); } } }
For information about the name EJB will use to bind to JNDI, refer to "JEUS EJB Guide".
In an HTML document, you can call a certain applet and specify the location of the applet's classes. In the following example, it is assumed that the previous application class and helloejb.Hello EJB interface are included in the hello-client.jar file. The jclient.jar is a JEUS client library, and it exists in the JEUS_HOME\lib\client directory.
[Example 3.2] <<index.html>>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello JavaEE</title>
</head>
<body>
<center>
<h1>Hello JavaEE Sample Applet!</h1>
<APPLET CODE = "helloejb.HelloClient" JAVA_CODEBASE = "."
ARCHIVE = "hello-client.jar,jclient.jar"
WIDTH = 300 HEIGHT = 300/>
</APPLET>
</center>
</body>
</html>
To use an HTML document in any browser, and JDK is not installed at the location where the browser is to be executed, you can induce the installation. To convert to an HTML document, use JDK's htmlconverter. For more details, refer to http://java.sun.com/j2se/1.5.0/docs/guide/plugin/developer_guide/html_converter.html.
Since an applet generally runs from an HTML document, a web application is required to transmit the HTML document to the web.
Before executing an applet, it is necessary to complete the deployment of the web application and EJB.
Web Application Deployment
Create a web application and include the JAR files specified in the HTML document and the ARCHIVE parameter. For details about web application creation and deployment, refer to "JEUS Application & Deployment Guide".
EJB Deployment
After deploying the web application, deploy the EJB application.
Execute the web browser, enter the URL of the HTML document, and execute the applet.
Following the Java Security model, an applet performs access control as specified in the java.policy file. Therefore, permission should be given to the class, used by the applet, in the java.policy file.
For information about the configuration settings in java.policy, refer to http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/security.html.
Access the web browser by using the <applet> tag in the HTML page. The applet in the example uses Swing, and so the following address is used to access the applet.
http://localhost:8088/hello/index.html