J2EE Code Behind for Flex / Actionscript Developers

Just re-stumbled upon Ted Patrick’s post on using the Code Behind technique in Flex with Actionscript. Ted wrote about how he used it in .NET and in Flex. There’re more explanations of Code Behind here and here.

The same technique exists in J2EE in the form of JSP and Java Beans. It’s also known as Model 1 architecture (Model 2 is an example of MVC). As Ted states MXML is just a bunch of tags that are really ActionScript code under the hood and a .mxml file is a .as file. JSP can be thought of as tags that represent Java code. They’re XML-like tags that the compiler breaks down into a Java class, specifically, a Servlet.

JSP is used for displaying visual content, what’s called the “View” in design pattern terminology. Java Beans are Java class files that handle the application’s logic.

You can use a JSP import statement to use a Bean to create objects and then access its public methods like so:

<jsp:useBean id="calc" class="com.mysite.MyCalc" scope="session" />

<html><head><title>JSP Code Behind</title></head><body>

	double myResult = calc.addTwoNumbrs(92.2231, 2384.0);
	out.print("myResult = " + myResult);
        //in the browser  you should see
        //myResult = 2476.2231


Here “id=calc” refers to the object reference, or an instance name called “calc.” The “com.mysite.MyCalc” part specifies the package “com.mysite” and class “MyCalc.” For the Tomcat 6 servlet container (or application server), this Bean would live inside the Tomcat6/webapps/yourApp/WEB-INF/classes/com/mysite/ directory.

package com.mysite;
public class MyCalc {
  public void MyCalc() { }
  public double addTwoNumbrs(double num1, double num2 )
     return num1 + num2;

One thought on “J2EE Code Behind for Flex / Actionscript Developers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s