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

An Account of Developing a Hybrid Java/Flex Application

09.05.2008
| 35220 views |
  • submit to reddit

Mariusz Saternus is a Polish Java Developer, currently working in the UK. He recently became interested in the idea of writing a hybrid Java/Flex application. He wrote an online PDF viewer with a Java back-end. This tutorial follows the steps he took during the development of the application. It is his first enterprise project and therefore it should not be seen as an expert's guide in this field (because it is not!) but an account of developing a hybrid Java/Flex application. You can contact him at mariusz AT idrsolutions DOT com. -- Geertjan Wielenga, Javalobby Zone Leader.

How does it work then?

You can see it for yourself at http://www.jpedal.org/FlexViewer/RC1/Viewer.html.

Use it to browse to a file on disk and then open it:

This is only version One to prove I could get it working. I've got big plans for later versions!

The viewer has two parts. The Java part, which handles all the hardcore PDF parsing and rendering, and the second part, the Flex front end, which is essentially a Flash GUI running in a browser. However, with Adobe AIR coming into play, there is no problem to turn it into a desktop application later.

So, how does it work internally? The user accesses the Flex application and selects the file he/she wants to view. That file is then uploaded to the server where the Java part of the application, in the form of a web service, is running. Next, using Java and a third party PDF library, we decode the page, get the needed information about the file and finally generate an image of the required page of the file we want to view. The link to that image is then sent back to the front end where the Flex app will fetch the image file from the server and display it to the user.

We have to use a third party PDF library simply because neither Flex nor Java provide the required functionality. There are a number of libraries out there to choose from: I'm going to go with JPedal because it is an easy to use API and has excellent rendering abilities. We'll also need a web server to run our Java code because we're going to expose it as a web service, for that reason I'll use Tomcat.

What tools am I going to need?

How to read this tutorial?

The tutorial is divided into two parts:

The parts are then divided into sections to reflect the structure of the project. I'll talk about each of the sections, commenting on important parts of the source code. At the end of each section there will be (if applicable) a link to a file with all the required code in it. The source code files will also contain comments. Whenever I mention things that need to be done in NetBeans, I'll highlight that text in blue, whereas operations performed in Flex Builder 3 will be highlighted in green. Last but not least, I'll be referring to the installation directory of tomcat as CATALINA_HOME throughout this tutorial and in the source code as well.

AttachmentSize
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.)

Comments

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...

 Peace.

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.