|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
POWERBUILDER LINKS YOU MUST CLICK ON PB & Java
Use Your PB Knowledge And Deploy Java Applets
By: Bob Hendry
Digg This!
This month we'll discuss how to build and deploy simple Java applets. By the end of this article, you should have the skill set needed to write, compile, deploy, and run simple Java applets. As you'll soon see, the coding portion of this is relatively easy. The real challenge is understanding the process, which will be my focus. Of course, I'll relate the material to the PowerBuilder knowledge you already know and love. The goal this month is to recognize the various components involved and the role they play. After this, understanding the code is relatively straightforward. Before starting, it's important to understand the different components and their roles in the development of applets. In the classic PowerBuilder client/server deployment model we're primarily interested in two things - creating the executable and deploying it on a computer. At this point we can map a shortcut to this file and, bada bing, the application is up and running. Writing Java applets is a bit more confusing than this. Before we can write and deploy an applet, it's important to understand the role of the four main components of applet deployment - the applet, HTML file, Web browser, and Web server.
Applet
HTML File
HTML consists of properties called tags. These tags tell the Web browser what the HTML page contains and how it looks. An HTML file can "request" an applet in one of these tags. If the HTML file learns (via a tag) that it needs to run a Java applet, the client notifies the server of the applet's name and where the server can find it. The applet is then "served" to the client where it's interpreted by the client's Web browser. The HTML file knows very little about the applet; it knows what Web server it lives on, how to ask for it, and where in the Web browser the user will see it run. The HTML file doesn't know anything about what the applet does or how it runs. HTML files don't need applets, but applets need HTML files. Applets cannot, will not, and never will be able to run without first being requested by an HTML file (I really set myself up, didn't I?). An HTML file may contain programmatic logic; however, the logic must be simple. For this reason HTML isn't considered a language.
Web Browser
Since the applet is run within the browser, Java programming with applets is often called client-side Java. There are a couple important things to point out here. First, the Web browser is the component that actually runs the applet. Many developers mistakenly say the applet runs on the client. While it's true that the applet currently exists on the local client machine, the applet isn't run on it; rather, it's run within the Web browser. So what's the difference? Plenty. By default, because of security reasons, an applet can't exist outside of the client Web browser. The browser keeps the applet insulated from the client system. The entire universe for the applet exists within the Web browser; it can't see any further. Since an applet can't see beyond the browser, it can't access the client system, including access to files, memory, printers, and the operating system. The area the browser allows the applet to run in is called the sandbox. Like its namesake, the sandbox provides a safe place to play. Like a toddler, an applet can wreak havoc. Because the browser allows the applet to access only the sandbox, the client computer is relatively safe from destructive applets. This security feature is needed to prevent an unscrupulous programmer from unleashing harmful applets into the world. The trade-off is that this strict security model severely limits what an applet can do.
Web Server
Building the Applet
// A very simple appletI won't provide an exhaustive list of Java syntax rules here, but I'll start by offering short and simple explanations and tips. // A very simple applet This is a comment and should look familiar. Commenting code in Java follows the same rules as commenting code in PowerBuilder. Single lines may be commented as in the foregoing. Multiple line comments must begin with a /* and end with a */, and everything between these symbols is considered comments. import java.awt.*;These lines specify which Java packages are included in the program. Since Java is a big language, objects within Java are organized into so-called packages that are related classes. If you're familiar with C, the Java import statement serves the identical purpose of the C include statement. This system is similar to how you might arrange objects in different PBLs. The main difference is that Java system classes are already put in these packages, whereas in PowerBuilder it's up to you to put objects in PBL files. When appropriate, I'll explain the basic contents of Java packages as we use them. By default the java.lang package is imported and includes all the basic rules and classes of the Java language. In the previous example, the awt and applet packages are imported. The * symbol means "give me all classes in the specified package." Later you'll see how to import individual classes rather than entire packages, but that's not important here. Import statements must end with a semicolon. Also, note that Java is case sensitive, so be careful when typing. public class Applet1 extends Applet { This is the class declaration line. Every Java program needs one, and it must appear immediately after the import statements. The first word identifies the scope (who can use) of the class. Notice how the name (Applet1) is in uppercase. This is a Java convention, to have all classes start with an uppercase letter. Note: An important Java rule is that the name of your file must be identical to the class name. For example, I'd have to name this file Applet1.java. Any other file name wouldn't even compile. The extends portion of the class declaration specifies which class ours is inherited from. You'll be pleased to know that Java uses inheritance the same way PowerBuilder uses it. In this case we're inheriting for the Java class Applet. As in PowerBuilder, your Java class doesn't have to be inherited from anything, so the extends portion is optional. Last, the "{" symbol starts your class block and must be terminated by a "}" and the end of the program. public void paint(Graphics g) {Above is the applet paint method. This method overrides an ancestor method of the same name. Java fires this method every time the applet needs to draw itself. The method has public access and returns nothing (void), an automatic method. This may be a new experience for you. In Java, some methods are wired to automatically fire on their own. In other words, Java is responsible for firing them - not you. Java is full of automatic methods such as paint. In PowerBuilder terms these automatic methods act more like events. There are other automatic methods in the applet class, and they'll be discussed in detail next month. The () holds the signature for the method. Java passes a graphics object to the paint method and we refer to it as g. At the end of the signature there's a "{", which starts our signature block. Similar to class blocks, signature blocks must be terminated by a "}". Also notice the indenting. Signature blocks are indented three characters from the class block. Three additional characters indent code in a signature block. This is not a Java requirement but makes the code more readable and easier to debug. Java is space insensitive - it ignores white space. The paint method contains only one line of code. The drawString method on the Graphics class is told to draw a string on the applet 50 pixels over and 25 pixels down. The Graphics class is used to draw text and simple shapes on the applet and will be discussed in more detail next month.
Compile the Applet
Follow the downloading and installation instructions. They're simple and easy to follow so I won't waste any space discussing them. Figure 1 shows a successful compile. After the compile, a class file will be generated that will soon be deployed to a Web server.
Create the HTML File
HTML Explained
Testing the Applet
AppletViewer Applet1.htm This runs the applet within the HTML file. If all is well, the contents of the Applet Viewer should look like Figure 2.
Deploying to a Web Server
ftp> open ftp.envisionsoft.com
About FTP
The FTP Commands
The Final Step
Final Words
All this Java development has probably made you thirsty. Since you have a new skill, have your boss take you out for a drink. While you're at it ask for a raise. After all, you deserve one.
Next Month
PBDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING POWERBUILDER / SYBASE NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||