Client Applets
This chapter describes how to create, configure, and execute a JEUS applet program.
1. Overview
An applet is a Java application that is executed in a web browser.
To use a Jakarta EE service in an applet, an applet container should be used. Because lightweight client containers are not provided yet, an applet that cannot directly access JEUS libraries is allowed to use a Jakarta EE service without a container.
With an applet, a user can execute Java applications in the web browser. An applet can be run as a JEUS client as well.
2. Programming
Files required to run an applet exist within a 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 shows sample examples.
2.1. Example
An applet should inherit the Applet or JApplet class and implement the start() method.
The following example shows a client without a client container. In this case, the client does not use dependency injection but JNDI API to look up an EJB.
In the example, the same EJB as the client container in the previous chapter is used, with the interface name, 'helloejb.Hello,' as its binding name.
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 more information about EJB JNDI binding name, see JEUS EJB Guide. |
2.2. HTML Example
In an HTML document, users can call a certain applet and specify the location of the applet’s classes.
In the following example, the application class and helloejb.Hello EJB interface in the previous example 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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello JakartaEE</title>
</head>
<body>
<center>
<h1>Hello JakartaEE Sample Applet!</h1>
<APPLET CODE = "helloejb.HelloClient" JAVA_CODEBASE = "."
ARCHIVE = "hello-client.jar,jclient.jar"
WIDTH = 300 HEIGHT = 300/>
</APPLET>
</center>
</body>
</html>
If an HTML document is used in any browser while JDK is not installed at the location where the browser is to be executed, you can prompt the user to install JDK. To convert to an HTML document, use JDK’s htmlconverter. For more information, refer to Java Plug-in HTML Converter. |
3. Deployment
Because an applet generally runs from HTML, running an applet in a browser requires a web application that passes an HTML document containing the applet to the browser. Therefore, it is necessary to complete the deployment of the web application and EJB before executing an applet.
-
Web Application Deployment
Create a web application and add the HTML document and the JAR files specified in 'ARCHIVE'. For more information about creating and deploying a web application, see JEUS Application & Deployment Guide.
-
EJB Deploy
After deploying the web application, deploy the EJB application.
4. Execution
Open the web browser, enter the URL of the HTML document, and run the applet. Following the Java Security model, an applet performs access control as specified in the java.policy file. Therefore, in the file, the classes that the applet uses must be given permission.
For information about the configuration settings in java.policy, refer to Security Developer’s Guide. |
4.1. Executing in Web Browser
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://host1:8088/hello/index.html
4.2. Executing in AppletViewer
For testing, you can execute an applet by using the AppletViewer included in the JDK, rather than the browser. Executing an applet by using the AppletViewer is more convenient for testing during development because users can immediately check for exceptions.
appletviewer index.html