Java

Team-Fly    

Macromedia® DreamWeaver® MX Unleashed
By Matthew Pizzi, Zak Ruvalcaba
Table of Contents
Chapter 21.  Working with CGI and Java Applets


If you cannot or do not want to use CGI or an application server, you may want to use Java applets to provide some of the functions that you can get from CGI. Java applets can be used for page counters, chat rooms, searching and news tickers, and much more.

Java is a programming language with a unique property. The same program file can run on any type of machine. A program written in Java, unlike any other language, can run on Windows PCs, Macs, Unix machines, PDAs even smart cards.

NOTE

At a 1998 Java conference, the delegates were all given a ring that ran a small Java program to remember their coffee preferences. The coffee machine also ran a Java program to read the preferences and served the appropriate black/white/sugar combinations to delegates (presumably coffee was a Java blend).


With traditional programs, after you have written the code, it gets compiled into an .exe file. This file contains all the machine-level code needed to run the program. This code is unique for the particular processing chip it was compiled for.

However, when you "compile" Java code, it does not produce machine-level code but something called bytecode in a file that has the extension .class. The processor does not read bytecode directly, but by a special software component called a Java Virtual Machine (JVM). JVMs exist for almost every computer platform and are usually installed as part of the operating system. Both Internet Explorer and Netscape Navigator browsers also contain a JVM.

The JVM contains a lot of the code needed for a Java program to interact with the operating system, so the bytecode file can be kept quite small. The capability to run on any machine and to keep code files small makes Java the perfect language for the Internet.

Small Java programs, called applets, can be written for use in Web pages. Your Web page can contain Java applets if you use the HTML <applet> tag. For security reasons, some things cannot be done with a Java applet. An applet cannot read or write files on the user's machine. It cannot start any other program on the user's machine or read many of the system properties. You will also notice that an applet window looks different from an operating system window, so that the user knows the window has come from your Web page.

The Java language itself is very strictly object oriented (see Chapter 20, "Extending Dreamweaver MX," for an introduction to objects) and is not a very easy language to learn if you have not done programming before. However, the community spirit of the Web comes the rescue again, because for most of the things you would want to use Java for, you can download an applet from the Web.

Because Java can run on any type of machine, it is used for both server-side and client-side programming. It can do some things within a client browser that are normally done server side, and if you cannot use CGI scripts, this is very useful. Dreamweaver MX gives you the capability to visually insert Java applets into your document.

Most Web designers will want to place a copy of the applet on their own server along with their HTML files, but it is also possible to use an applet that is kept on another server. You will use a remote applet for the first Java exercise the creation of a chat room.

Using a Java Chat Room

Many companies offer chat room facilities. They are usually free, but in return, your chat pages will show an advert. One company offering chat rooms is ParaChat. It offers both free and paid-for chat rooms. The chat room works by running Java both on the company's own server, to store the responses by the chatters, and on your Web page, to display the chat conversations to your users. To use the free chat, go to http://www.parachat.com and follow the links to the free chat rooms. In this exercise, choose the HTML Chat option.

Figure 21.10. The ParaChat Web site gives you simple chat rooms for free. If you wanted a chat room with more powerful capabilities, you can also purchase one from them.

graphics/21fig10.jpg

You need to fill in a form to register, and afterward, you will be sent an email with the HTML code you need to include in your Web page. You can paste this code into a page on your Web site using code view. This email also gives you a URL to a page that shows you the parameters required to customize your chat applet. If you switch to design view, you will just see a large gray rectangle with a coffee cup logo on it. You can resize the applet to fit within your design and you can also do some basic formatting by setting parameters as described in the next section.

Figure 21.11. After pasting in the ParaChat code, you can see the applet icon in design view.

graphics/21fig11.jpg

Setting Property Parameters

Most Java applets allow some degree of customization using parameters, and the ParaChat applet is no exception. The parameter names will be different for each applet, and you will need to refer to the applet's documentation to find the names to use and the options available.

To set parameters in Dreamweaver MX, click the applet itself. The Properties palette, as shown in Figure 21.12, will change to show the Java applet properties. Many of these properties have already been set with the HTML code you pasted in. Table 21.3 shows a list of the properties you can set using Dreamweaver.

Figure 21.12. The Properties palette lets you set the applet properties.

graphics/21fig12.jpg

Table 21.3. Java Applet Properties
Property Description
Name You need to fill this in only if you are going to use JavaScript behaviors on your applet. Most of the time it can be left blank.
W The width of the applet in pixels as it will be displayed on the page. It can be changed by dragging with the mouse as well as by entering new values into this box.
H The height of the applet in pixels as it will be displayed on the page. Both W and H are like the W and H properties of an image.
Code Specifies the name of the file of the Java applet. It will have the extension .class. By clicking the icon, you can browse to this file.
Base By default, the browser expects the code file to be in the same directory as the HTML file it is included in. If the .class file resides in another directory, or on another server, the URL to that location should be entered here. This can be a relative or an absolute URL. If Dreamweaver enters it for you after you browsed to the file, you should check it carefully to make sure that it matches the location on your remote server.
Align This is how the applet is aligned on the page, relative to the rest of the text on the page. It acts just like the align attribute of images.
Alt Alternative text or an image that displays if Java has been disabled. To choose an image click the folder button and browse to the image file.
V Space, H space The white space around the applet, just like V Space and H Space with images.

Near the bottom of the Properties palette is a button marked Parameters. Clicking this opens up a small dialog box, as shown in Figure 21.13. You can see that some parameters have already been set and you can add your own after referring to the documentation. To add a parameter, click the plus (+) button and then fill in the parameter's name and its value.

Figure 21.13. Add a parameter by clicking the plus (+) button in the Parameters dialog box.

graphics/21fig13.jpg

If you now preview the page, you should be able to test out the chat applet.

NOTE

ParaChat is one of a number of free chat rooms available. Although they offer slightly different interfaces and functions, they all are inserted in your Web page the same way.

Users that view Web pages from behind a firewall will probably have problems using chat applets. Firewalls are rarely configured to allow chat communications through.


Applets Versus CGI Scripts

For some functions, such as chat rooms and search engines, both Java applets or CGI scripts can be used. Remember that applets work on the client side, whereas CGI scripts run on the server. You may also want to consider the following:

  • Java applets have an advantage in that you do not need to make any changes to the server. If you are not allowed to configure your server or use CGI, applets are your only option. It is usually easier to insert an applet in the page than it is to configure the server. If you are nervous about messing around with a Web server, you may prefer to use an applet.

  • Applets will work only if the user has a Java-compatible browser (nearly all browsers in use today support Java) and they have Java enabled (it is enabled by default). Some corporations do not allow their browsers to run Java for security reasons. CGI scripts, because they do all the processing on the server, do not violate security restrictions.

  • Applets generally have more limited functionality than CGI scripts do. For database access, for instance, you would almost certainly need to use CGI or an application server.

Project: Inserting a Search Engine into an HTML Document

There are many ways of providing a search engine with CGI or application servers, but if you can't use CGI, you can use a Java applet. Browsing through http://www.freewarejava.com, we found a search applet called sitesearch (http://www.lundin.info/search.asp). This applet seemed to work best and have the most options of the many applets listed, but the other applets all work on the same principles. Follow these steps:

  1. Download the applet and print off the documentation for reference later.

  2. After unzipping the downloaded file, copy the search.class file into the top level of your site folder (and remember to upload to your testing server and remote server).

  3. You can copy the applet tag example that is given in the documentation or insert the applet using the Insert menu (Insert+Media+Applet) or the Media tab of the Insert bar in Dreamweaver.

  4. If Dreamweaver inserts a directory name in the Base property, delete it. As a minimum, you need to set the firstpage parameter this is the page that the applet will start searching from. It searches through the pages by following hyperlinks from this first page specified. See Figure 21.14 to see the applet in design view.

    Figure 21.14. Click the Applet icon in Dreamweaver's design view. You can see the Applet properties, and yours should match those shown here.

    graphics/21fig14.jpg

There are many other options that can be set to make the applet match your page colors and to set the text displayed in the applet. See the documentation for details.


    Team-Fly    
    Top


    Macromedia Dreamweaver MX Unleashed
    Macromedia Dreamweaver MX 2004 Unleashed
    ISBN: 0672326310
    EAN: 2147483647
    Year: 2002
    Pages: 321

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net