Professional Java Tools for Extreme Programming [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Professional Java Tools for Extreme Programming [Electronic resources] - نسخه متنی

Richard Hightower, Warner Onstineet al.

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید











The Applet Project


The applet project is a simple applet that reads the output of the HelloWorldServlet (defined in Chapter 6) and shows it in a JLabel. The dependency on the Web application is at run time; there are no compile-time dependencies to the Web application. We'll discuss the applet here and the Web application 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 application project buildfile.

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

/ 228