Chapter 3. Client Applets

Table of Contents

3.1. Overview
3.2. Programming
3.2.1. Example
3.2.2. HTML Example
3.3. Deployment
3.4. Execution
3.4.1. Executing in Web Browser
3.4.2. Executing in Appletviewer

This chapter describes how to create, configure, and execute a JEUS applet program.

3.1. Overview

An applet is a Java application that is executed in a web browser. To use a Java 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 Java 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.

3.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.

3.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.

[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();
        }
    }
}


Note

For information about the name EJB will use to bind to JNDI, refer to "JEUS EJB Guide".

3.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.

[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>

Note

If an HTML document is used in any browser while JDK is not installed at the location where the browser is to be executed, users can install JDK. To convert to an HTML document, use JDK's htmlconverter. For more details, refer to http://docs.oracle.com/javase/1.5.0/docs/guide/plugin/developer_guide/html_converter.html.

3.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 include the JAR files specified in the HTML document and the ARCHIVE parameter. For details about creating and deploying a web application, refer to "JEUS Application & Deployment Guide".

  • EJB Deployment

    After deploying the web application, deploy the EJB application.

3.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.

Note

For information about the configuration settings in java.policy, refer to http://docs.oracle.com/javase/1.5.0/docs/guide/deployment/deployment-guide/security.html.

3.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

3.4.2. Executing in Appletviewer

For testing, it is possible to execute an applet by using the Appletviewer included in the JDK, rather than the browser. Executing an applet by using the Appletviwer is more convenient for testing during development because users can immediately check for exceptions.

appletviewer index.html