JaVa
   

The Applet Project

The applet project is a simple applet that reads the output of the HelloWorldServlet (defined in ) and shows it in a JLabel. The dependency on the Web app is at run time; there are no compile-time dependencies to the Web app. We'll discuss the applet here and the Web app in the next chapter.

Overview of the Applet Class

The meat of the applet implementation is in the init() method, as follows:

 public void init(){
 URL uGreeting;
 String sGreeting="Bye Bye";
 getAppletContext()
.showStatus("Getting hello message from server.");
 try{
 uGreeting = new URL(
 getDocumentBase(),
 "HelloWorldServlet");
 sGreeting = getGreeting(uGreeting);
 }
 catch(Exception e){
 getAppletContext() .showStatus("Unable to communicate with server.");
 e.printStackTrace();
 }
 text.setText(sGreeting);
 }


The init() method gets the document base (the URL from which the applet's page was loaded) URL from the applet context. Then, the init() method uses the document base and the URI identifying the HelloWordServlet to create a URL that has the output of the HelloWorldServlet:

uGreeting = new URL( getDocumentBase(), "HelloWorldServlet");


It uses a helper method called getGreeting() to parse the output of the HelloWorldServlet, and then displays the greeting in the Applet's JLabel (text.setText(sGreeting);). The helper method is as follows:

 private String getGreeting(URL uGreeting)throws Exception{
 String line;
 int endTagIndex;
 BufferedReader reader=null;
 . . .
reader = new BufferedReader(
 new InputStreamReader (
 uGreeting.openStream()));
 while((line=reader.readLine())!=null){
 System.out.println(line);
 if (line.startsWith("<h1>")){
 getAppletContext().showStatus("Parsing message.");
 endTagIndex=line.indexOf("</h1>");
 line=line.substring(4,endTagIndex);
 break;
 }
 }
 ...
 return line;
 }


Basically, the method gets the output stream from the URL (uGreeting.openStream()) and goes through the stream line by line looking for a line that begins with <h1>. Then, it pulls the text out of the <h1> tag. The output of the HelloServlet looks like this:

<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>


The code that the helper method retrieves appears in bold. The following listing shows the complete code for HelloWorldApplet.

package xptoolkit.applet;
import javax.swing.JApplet;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.BorderLayout;
import java.applet.AppletContext;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class HelloWorldApplet extends javax.swing.JApplet {
 JLabel text;
 public HelloWorldApplet() {
 this.getContentPane().setLayout(new BorderLayout());
 text = new JLabel("Bye Bye");
 text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
 text.setAlignmentY(JLabel.CENTER_ALIGNMENT);
 Font f = new Font("Arial", Font.BOLD, 20);
 text.setFont(f);
 getContentPane().add(text,BorderLayout.CENTER);
 }
 public void init(){
 URL uGreeting;
 String sGreeting="Bye Bye";
 this.doLayout();
 getAppletContext()
.showStatus("Getting hello message from server.");
 try{
 uGreeting = new URL(
 this.getDocumentBase(),
 "HelloWorldServlet");
 sGreeting = getGreeting(uGreeting);
 }
 catch(Exception e){
 getAppletContext()
.showStatus("Unable to communicate with server.");
 e.printStackTrace();
 }
 text.setText(sGreeting);
 }
 private String getGreeting(URL uGreeting)throws Exception{
 String line;
 int endTagIndex;
 BufferedReader reader=null;
 try{
 reader = new BufferedReader(
 new InputStreamReader (
 uGreeting.openStream()));
 while((line=reader.readLine())!=null){
 System.out.println(line);
 if (line.startsWith("<h1>")){
 getAppletContext().showStatus("Parsing message.");
 endTagIndex=line.indexOf("</h1>");
 line=line.substring(4,endTagIndex);
 break;
 }
 }
 }
 finally{
 if (reader!=null)reader.close();
 }
 return line;
 }
}


Creating a Buildfile for the Applet

The applet project buildfile is quite simple, as shown in the following listing; it is structured much like the app project buildfile.

<project default="all" >
 <target unless="setProps" description="setup the properties.">
 <property value="/tmp/app" /> </target>
 <target depends="setProps" description="initialize the properties.">
 <tstamp/>
 <property name="local_outdir" value="${outdir}/applet" />
 <property value="${local_outdir}/classes" />
 <property value="${outdir}/lib" />
 <property value="${lib}/helloapplet.jar" />
 </target>
 <target depends="init" description="clean up the output directories.">
 <delete dir="${build}" />
 <delete dir="${jar}" />
 </target>
 <target depends="init" description="prepare the output directory.">
 <mkdir dir="${build}" />
 <mkdir dir="${lib}" />
 </target>
 <target depends="prepare" description="compile the Java source.">
 <javac srcdir="./src" destdir="${build}" />
 </target>
 <target depends="compile"
 description="package the Java classes into a jar.">
 <jar jarfile="${jar} "
 basedir="${build}" />
 </target>
 <target depends="clean,package" description="perform all targets."/> </project>


Building the Applet with Ant

To build the applet, we need to navigate to the Applet directory, set up the environment, and then run Ant at the command line. To clean the output from the build, we run "ant clean" at the command line. Both building and cleaning the applet are demonstrated as follows. First we build the applet:

C:\CVS\...\MVCHelloWorld\Applet>ant Buildfile: build.xml setProps:
init:
clean:
 [delete] Deleting directory C:\tmp\app\lib prepare:
 [mkdir] Created dir: C:\tmp\app\applet\classes
 [mkdir] Created dir: C:\tmp\app\lib compile:
 [javac] Compiling 1 source file to C:\tmp\app\applet\classes package:
 [jar] Building jar: C:\tmp\app\lib\helloapplet.jar all:
BUILD SUCCESSFUL Total time: 4 seconds


Now we clean the applet:

C:\CVS\...\MVCHelloWorld\Applet>ant clean Buildfile: build.xml setProps:
init:
clean:
 [delete] Deleting directory C:\tmp\app\applet\classes
 [delete] Deleting directory C:\tmp\app\lib BUILD SUCCESSFUL Total time: 0 seconds


JaVa
   
Comments