Integrating Client-Side Java Applets

Java applets are commonly used for user interface elements such as scrolling tickers, advertisement animations, charts, and so forth. The advantage of Java applets over ActiveX controls is their breadth of deployment. In theory, you can write a Java applet once and deploy it anywhere. In practice, Java applets that are intended to run in multiple types of browser require a lot of testing to ensure consistent functionality across all browser platforms. It is therefore up to you as a developer to understand the trade-offs involved in choosing to go with ActiveX controls or Java applets.

ActiveX controls are generally easier to write but are restricted somewhat in their browser support. Java applets, on the other hand, take a little longer to develop (arguably) and require more testing but can run on a wider variety of browsers and platforms.

Ultimately, the decision rests upon the target audience for your applications. If you are developing for the Internet, where the client-side is unknown, it's often best to go with Java applets or even plain HTML. If you are developing for an intranet or extranet, where the client-side is known or can be controlled more easily, it's often best to go with ActiveX controls or plain HTML. In recent years, there has been somewhat of a push-back on both ActiveX controls and Java applets, and organizations are often opting for the more straightforward HTML deployment option, especially for their Internet and extranet applications. For intranet applications, ActiveX controls and Java applets can give your end users the interactivity they need without causing too much stress for developers since the client platform is known and controlled.

Inserting Java Applets into HTML and ASP Files

The easiest way to integrate a Java applet into an HTML or ASP Web page is to manually code the <APPLET> tag directly into the Visual InterDev Source View editor. If you plan to reuse the <APPLET> code, you can highlight the code in the Source View editor and drag it onto the Toolbox to create a new item. You might also want to create a new tab named Java Applets prior to dragging the applet code onto the Toolbox so that you have a way to organize your applets and keep them separate from your ActiveX controls.

If you add a Java applet to the Toolbox, be sure to rename the item so that it is more meaningful. By default, applets are given the name HTML Fragment, so you'll want to change the name to reference your specific applet. Figure 11-7 shows an applet in the Source View editor and its corresponding item in the Toolbox. The item was named ChartGear Sample after the name of the Java applet.

click to view at full size.

Figure 11-7. A Java applet in the Source View editor and shown in the Java Applets tab in the Toolbox.

NOTE
The ChartGear applet is available from Solstice Inc. on their Web site at http://www.solsticeinc.net. This applet can be used for creating graphs and has been rated in the top five percent of applets on the Web by the Java Applet Rating Service (JARS).

Applets are similar to ActiveX controls in that they also use <PARAM> tags to specify additional properties. The following code shows an example applet within an HTML page:

<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <H2>Sample Applet</H2> <HR> <APPLET code=ChartGear.class id=SBSChart width="550"      height="225" VIEWASTEXT>     <PARAM name=Num_Items value="7">     <PARAM name=Data(1) value="6.4">     <PARAM name=Data(2) value="11.7">     <PARAM name=Data(3) value="12.9">     <PARAM name=Data(4) value="14.5">     <PARAM name=Data(5) value="9.1">     <PARAM name=Data(6) value="10.0">     <PARAM name=Data(7) value="1.5"> </APPLET> </BODY> </HTML> 

This code is taken from the Java Sample.htm page in the CHAP11 folder on the CD-ROM. The code displays the ChartGear applet, as shown in Figure 11-8.

click to view at full size.

Figure 11-8. The Java Sample.htm page showing a graphing applet.

When deploying applets, you might also need files that the applet depends on. The applet may have a number of additional .class files that you need to include on your Web server and in your development environment. Class files are the machine-independent byte code generated from .java files by the javac compiler in the JDK or by another compiler in a visual Java development environment such as Microsoft Visual J++. When deploying Java applets, you must be aware of the applet's requirements in terms of required files and make sure you meet them.

As with ActiveX controls, you can switch to viewing the graphical representation of your Java applets within the Source View editor by right-clicking the applet section of the code and unchecking Always View As Text on the context menu.

Scripting Java Applets

One of the interesting uses of ASP server-side scripting is to dynamically generate parameters for Java applets before you send the Web page to the browser. For instance, using ASP, you can query a database and then dynamically map the returned results into client-side <PARAM> tags for the applet so that the variables are sent down to the client with the applet. You can use a Chart applet that charts data in this manner—by setting the chart values to equal values coming from a database. This is easy to do; you simply use the <%=varname%> expression or a similar expression within the appropriate <PARAM> tags for the applet in the ASP Web page. An ASP Web page can contain any number of Java applets.

As an example of how to script Java applets within Visual InterDev, we'll use a sample ASP Web page that builds a dynamically created graph, as described earlier. The page is named Funds.asp and is included on the CD-ROM under the CHAP11 folder.

The example uses a graphing applet so that the end user can see his or her 401K investment fund performance by twelve-month period and by two-week period. The data is pulled from the database for that specific user and is output both as <PARAM> tags for the Java applet and as an HTML table below the applet. The user is also given the option of being able to change the chart display type and the periods to be graphed. In addition to supplying the data for the fund performance graph, the database query also supplies the data for some of the graph labels, such as the period ending date. A Recordset DTC is used to perform the query within the ASP Web page.

Figure 11-9 shows the Funds.asp page as it appears in the Netscape Communicator 4.01 browser. This browser was chosen to illustrate that the code is functional both within Internet Explorer and within Communicator.

The code below shows the source code for the Funds.asp page. The code for the Recordset DTC has been edited so that it shows the SQL query only. Also, the code that displays the HTML table with all the fund performance values at the bottom of the Web page has also been removed for the sake of brevity. As you can see from the code, the <PARAM> tags for the applet are created using a loop over each record in the recordset. This allows the resulting graph to contain however many rows are returned from the query. In the query, you'll see that there are six funds selected.

click to view at full size.

Figure 11-9. The Funds.asp page showing the dynamically created data displayed in the Fund Performance graph. The graph uses the ChartGear Java applet.

<%@ LANGUAGE="VBSCRIPT" %> <% ' VI 6.0 Scripting Object Model Enabled %> <!--#include file="_ScriptLibrary/pm.asp"--> <% if StartPageProcessing() Then Response.End() %> <FORM name=thisForm METHOD=post> <HTML> <HEAD> <TITLE>&quot;Pandora&quot; 401K Investment Fund Information</TITLE> </HEAD> <BODY> <!--METADATA TYPE="DesignerControl" startspan … <Recordset DTC> … cmdTmp.CommandText = 'SELECT id, Date_start, Date_end, fund_a,      fund_c, fund_d, fund_e, fund_f, fund_g FROM FundPerformance      WHERE Date_End IN (SELECT Max(Date_End) FROM FundPerformance)     ORDER BY id, Date_end' … <!--METADATA TYPE="DesignerControl" endspan--> <CENTER> <% DataCommand1.MoveLast %> <H2>401K Fund Performance - Period Ending <% =      CStr(DataCommand1.fields.getValue("Date_End")) %></h2> <% DataCommand1.MoveFirst %>  | <i><a HREF="funds.asp">Latest Performance</a></i>  | <i><a HREF="funds_history_2wk.asp">2-Week Period Rates of Return</a></i>  |  <i><a HREF="funds_history.asp">12-Month Period Rates of Return</a></i>  |  <i><a HREF="preferences.asp">Preferences</a></i>  | </CENTER> <HR> <TABLE> <TR> <TD> <APPLET code="ChartGear.class" align="baseline" width="500"      height="300" id="graph1" VIEWASTEXT> <% counter = 1 DO WHILE NOT DataCommand1.EOF cnt = 1 %> <PARAM name="Data(<% = counter %>,<% = cnt %>)"      value="<% = DataCommand1.fields.getValue("Fund_A") %>"> <% cnt = cnt + 1 %> <PARAM name="Data(<% = counter %>,<% = cnt %>)"      value="<% = DataCommand1.fields.getValue("Fund_C") %>"> <% cnt = cnt + 1 %> <PARAM name="Data(<% = counter %>,<% = cnt %>)"      value="<% = DataCommand1.fields.getValue("Fund_D") %>"> <% cnt = cnt + 1 %> <PARAM name="Data(<% = counter %>,<% = cnt %>)"      value="<% = DataCommand1.fields.getValue("Fund_E") %>"> <% cnt = cnt + 1 %> <PARAM name="Data(<% = counter %>,<% = cnt %>)"      value="<% = DataCommand1.fields.getValue("Fund_F") %>"> <% cnt = cnt + 1 %> <PARAM name="Data(<% = counter %>,<% = cnt %>)"      value="<% = DataCommand1.fields.getValue("Fund_G") %>"> <% cnt = cnt + 1 %> <% counter = counter + 1 %> <% DataCommand1.MoveNext %> <%  LOOP %> <PARAM name="Num_Items" value="<% = cnt - 1 %>"> <% num_sets = counter - 1 %> <PARAM name="Num_Sets" value="<% = counter - 1 %>"> <% DataCommand1.MoveFirst %> <PARAM name="Set_Name(1)" value="12-Month Period"> <PARAM name="Set_Name(2)" value="2-Week Period"> <% cnt = 1 %> <PARAM name="Item_Name(<% = cnt %>)" value="Fund A"> <% cnt = cnt + 1 %> <PARAM name="Item_Name(<% = cnt %>)" value="Fund C"> <% cnt = cnt + 1 %> <PARAM name="Item_Name(<% = cnt %>)" value="Fund D"> <% cnt = cnt + 1 %> <PARAM name="Item_Name(<% = cnt %>)" value="Fund E"> <% cnt = cnt + 1 %> <PARAM name="Item_Name(<% = cnt %>)" value="Fund F"> <% cnt = cnt + 1 %> <PARAM name="Item_Name(<% = cnt %>)" value="Fund G"> <PARAM name="Legend_Color" value="white">  <PARAM name="Legend_BG_Color" value="black">  <PARAM name="Legend_Location" value="Bottom"> <PARAM name="Title" value="MAP Fund Performance"> <% DataCommand1.MoveLast %> <PARAM name="Set_Label" value="Period Ending <% =      CStr(DataCommand1.fields.getValue("Date_End")) %>"> <PARAM name="Mag_Label" value="Percentage"> </APPLET> </TD> <TD valign=top> <TABLE> <TR> <TD align="center" bgcolor="#0000FF">     <FONT color="#FFFFFF">Select Period</FONT> </TD> </TR> <TR> <TD valign="top" width="175"><FONT size="2"> <INPUT type="radio" name="Set" value="Set2"      onclick="if(document.graph1 != null) {         document.graph1.set_Area_Of_Interest(1,1);         document.graph1.refresh();     }">Focus on 12-Month Period<BR> <INPUT type="radio" name="Set" value="Set1"      onclick="if(document.graph1 != null) {         x = 2         y = 3            document.graph1.set_Area_Of_Interest(2,2);         document.graph1.refresh();     }">Focus on 2-Week Period           <INPUT type="radio" name="Set" value="SetAll"      onclick="if(document.graph1 != null) {         document.graph1.set_Area_Of_Interest(1,2);         document.graph1.refresh();     }">Display ALL Data</FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE> <BR> </P> <HR> … <Code to display the HTML table> … <HR> Be sure to visit the rest of our site! <%  DataCommand1.Close  %> </BODY> <% ' VI 6.0 Scripting Object Model Enabled %> <% EndPageProcessing() %> </FORM> </HTML> 

The page also contains some code that allows the graph to be changed dynamically by the end user. A series of radio buttons allow the end user to select the area of interest to graph. This can be fund performance for the two-week period, the twelve-month period, or both periods.

While there is quite a lot of code within this sample, once you have developed such a page you have a very flexible and powerful way of graphing data dynamically from the database. This single ASP Web page can provide customized graphs to end users and can even be customized further to display only those fund types that the user wants to see. One way to achieve this customization is via conditional logic in the ASP Web page and the use of cookies to store user preferences.

The trick to sucessfully integrating Java applets into your ASP Web pages is in learning all the events, methods, and properties that are available to you within the applet itself. Once you know how it can be manipulated and what parameters it requires, it's relatively easy to write the ASP code or other script code for your applications. Be sure to use the Script Outline window within Visual InterDev to assist you in your scripting. Also, when working with Java applets, you'll want to be sure to test the applet in whatever browsers you need to support.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

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