Sending Form Data to a JSP
Now since the formula for converting Fahrenheit to Celsius is a matter of simple arithmetic, we were able to perform the conversion within the application. However, let's look at the code that we would use to let a server perform the calculation for us.
In version 1.2 of the Temperature Converter, we're going to instruct the Flash movie to send the Fahrenheit value to a remote JSP server, perform the calculation on the server-side, and return the value back to the Flash movie (for this example, I used the Jakarta Tomcat 5.0.19 as the JSP engine). Following is the ActionScript code that sends the Fahrenheit value to the Tomcat server, and gets the Celsius value back:
function tempConverter(){ loadVariablesNum("http://localhost:8080/convert.jsp?ftemp=" + fvalue, 0); }
Listing 1 shows the JSP code that accepts the Fahrenheit value to perform the conversion.
<% String ftemp = request.getParameter("ftemp"); int f_int = Integer.parseInt(ftemp); double cvalue = (f_int - 32) * 5 / 9; out.println("cvalue="+ cvalue); %>
Listing 1: convert.jsp
So, if you enter "212" as the Fahrenheit value, then the URL String that would be sent to convert.jsp would look like this:
http://10.0.1.15:8080/convert.jsp?ftemp=212
After this is executed, the JSP will return a plain text file like this:
cvalue=100.0
Since our Flash movie has defined cvalue as a variable, all the JSP had to do was to return a name-value pair for cvalue without any HTML formatting. Of course, if we wanted the JSP to return more than one value back to the movie, then the values must be separated by ampersands. Figure 8 shows the final version of the Temperature Converter application.
Figure 8: Temperature converter running on an iPAQ 4155
Conclusion
So, what can we take away from all this? As you can recall from Table 2, Flash carries over several good features from Java, including platform independence. On the PocketPC platform, Flash applications have a distinct advantage over Java 2 Micro Edition (J2ME) Connected Device Configuration (CDC) applications because Flash can be executed locally or provisioned over a network connection. We've also learned that Flash movies have the capability to load external data, either from a property file or via a URL. One of the most powerful features that we've seen is the capability of Flash to send and receive data from a JSP.
Now that you know that Flash has several mobile and enterprise features built-in, who knows, maybe it will be the platform of choice for your next project!