Mariusz has posted 1 posts at DZone. View Full User Profile

An Account of Developing a Hybrid Java/Flex Application

  • submit to reddit

The Java Side of Things

As mentioned before, to expose the functionality of the components running on the server we'll use a Web Service. In the next step I'll explain how to setup a Web Application project in NetBeans 6.1. Those of you who are familiar with this aspect of Java EE can probably skip the next section.

Creating The Web Service

In this section I shall explain how to set up a Web Application project in NetBeans and add a Web Service to it. To create a new Web Application project click: File->New Project ... a window will appear (screen shot below), at this stage make sure you have selected Web in Categories and Web Application in Projects.

With the correct options selected, click Next. On the next screen, provide a name for the project. In my case, it is JPedal_Server. Apart from that there is no need to change anything else in this section. Click Next. On the new screen make sure you have Tomcat 6.0.16 selected as Server and Java EE 5 as your Java EE Version just like on the screen shot below.

With the correct selections made, click Finish. There is one more screen we could have gone through but it refers to frameworks available to our project, which we won't be making any use of. After the Finish button has been clicked you will notice that the main window in the IDE now displays the content of the index.jsp file. Next step is to add a Web service to our application. To do this you need to right-click the main project node, select New and from the following menu select Web Service.

Once you click the Web Service icon you will be presented with another dialogue window. Here, just provide the Web Service name, in my case PDFImage, and a package, say org.maz. Next, click Finish. A new tab will open in the main window where you will see your Web Service in a design view.

Before we start adding actual code to our service we need to do one more thing. As I mentioned before, we're going to let the Java PDF library deal with all the hardcore PDF parsing and rendering. To be able to do that we need to add the JPedal jar to the CLASSPATH of the project. In NetBeans this is done by right-clicking the main project node and selecting Properties form the menu. After that, we are greeted with another dialogue panel, there we should select Libraries from the Categories list and then click the 'Add JAR/Folder' button. This will allow us to find and select the needed jar.

When done, click OK.

tutorial_html_2e51c903.jpg44.67 KB
tutorial_html_4d0d1668.jpg54.26 KB
tutorial_html_5717e36d.png9.22 KB
tutorial_html_4439460e.png12.88 KB
tutorial_html_m36babc34.gif28.69 KB
tutorial_html_m62eed3d8.png10.66 KB
tutorial_html_m9807edd.jpg41.49 KB
tutorial_html_m38373e52.jpg27.05 KB
tutorial_html_m9768552.png14.54 KB
flex-pdf-viewer.png67.61 KB
PDFImage.java3.62 KB
fileUpload.txt1.63 KB
crossdomain.txt298 bytes
Published at DZone with permission of its author, Mariusz Saternus.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Brian Matzon replied on Mon, 2008/09/08 - 3:11am

It broke my back button ;)

This looks more like a component description thana breakdown on what went well and what didn't.

Why go for flex ? - why not an applet?

Why use java as backend ?

Mariusz Saternus replied on Mon, 2008/09/08 - 4:14am

The reason for not using an applet is because I wanted to avoid a Java dependency on the client in this case - I wanted to use Flex.

If you have an Flex RIA and you want to say display a pdf file you just generated to the user you can't just use an applet. There is no way Flex will be able to 'talk' to the applet. That is what I was interested in - finding ways of communicating between the two technologies. 

The reason for using Java ... well I'm a Java programmer so the choice was obvious. Java is one possible option which may or may not suit everyone.

The article was never meant to be a "breakdown on what went well and what didn't". When I was working on the project I found it hard to find information about the things that I was working on and I wanted to share my findings with others.

Srinivas Veeravalli replied on Tue, 2008/09/09 - 12:27am in response to: Mariusz Saternus

Flex and Java applets can be used in a mixed environment. There are, however, challenges:

  • Making the applet visible. You can do this with a floating iframe, and with the applet as the source of the iframe. You need to set wmode to opaque in Flex for the iframe (and applet) to appear. Else they will go underneath the Flex rectangular area, and will never be visible.
  • Once you achieve the above, there are still z-ordering issues. Essentially the applet will "eat up" all Flex-based visual entities (mouse gestures,Flex dialogs, etc) which overlap with the applet area. There are ways to work around this problem, but these are not very pretty.
  • Finally the communication: you can use JScript (on the Java side) and ExternalInterface (on the Flex side). The glue would of course be JS functions.
So it can be done. Question is whether it is aesthetic enough.

Mariusz Saternus replied on Tue, 2008/09/09 - 12:59am in response to: Srinivas Veeravalli

Thanks for the info. I wasn't aware of such possibilities. There would still be one problem though, what it the users has disabled JS ? Cheers!

Srinivas Veeravalli replied on Tue, 2008/09/09 - 2:59am in response to: Mariusz Saternus

Hmmm.. how would your Flex program run then? AC_OETags.js ring a bell?

 Cheers :-)

Srinivas Veeravalli replied on Tue, 2008/09/09 - 3:02am in response to: Srinivas Veeravalli

Sorry, you are right - it is possible to run Flash without JS.

 But JS presence is a valid assumption in today's Ajax world, I suppose...


Dario Herrera replied on Mon, 2008/09/15 - 6:13am

take a look of Fiji: Exadel Fiji extends JSF by allowing the use of Flex with JSF components and within a JSF page....

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.