Tuesday, November 01, 2005

Scripting graphics and script language scriptlets in JSP

Ditchnet's 2D taglib allows a user to incorporate Java 2D API graphics into their web pages without applets, flash or any other fancy accessories.

I was looking through some JSP graphics tag libraries. I found JFreeChart and CeWolf which for future reference look to be really useful charting libraries. Then I discovered Ditchnet's 2D taglib via a link to another Ditchnet offering an XHTML/CSS menu tabs taglib.

At sometime in the future browser vendors will agree upon a common standard for web graphics using SVG or WHATWG's Canvas tag or some other new standard. Until that time this tag library can enable cross platform dynamic graphics. A slight drawback of the 2D taglib is that it appears to require a 2.4 servlet container (e.g. Tomcat 5+).

I wouldn't describe myself as a AWT programmer but using this tag library was quite easy. I located an example of using the Java 2D API in Sun's Java Tutorial in the section on Constructing Complex Shapes from Geometry Primitives. Adapting the tutorial's example I developed my script thus:


<%@ taglib prefix="twod" uri="http://ditchnet.org/jsp-2d-taglib" %>
<twod:canvas id="pear" width="120" height="120" border="1px" alt="A pear">

importClass(java.awt.geom.Ellipse2D);
importClass(java.awt.geom.Area);
importClass(java.awt.Dimension);
importClass(java.awt.Color);

var circle = new Ellipse2D.Double();
var oval = new Ellipse2D.Double();
var leaf = new Ellipse2D.Double();
var stem = new Ellipse2D.Double();

var circ = new Area(circle);
var ov = new Area(oval);
var leaf1 = new Area(leaf);
var leaf2 = new Area(leaf);
var st1 = new Area(stem);
var st2 = new Area(stem);

var w = 120;
var h = 120;
var ew = w/2;
var eh = h/2;

g.setColor(Color.green);

// Creates the first leaf by filling the intersection of two Area objects created from an ellipse.

leaf.setFrame(ew-16, eh-29, 15.0, 15.0);
leaf1 = new Area(leaf);
leaf.setFrame(ew-14, eh-47, 30.0, 30.0);
leaf2 = new Area(leaf);
leaf1.intersect(leaf2);
g.fill(leaf1);

// Creates the second leaf.

leaf.setFrame(ew+1, eh-29, 15.0, 15.0);
leaf1 = new Area(leaf);
leaf2.intersect(leaf1);
g.fill(leaf2);

g.setColor(Color.black);

// Creates the stem by filling the Area resulting from the subtraction of two Area objects created from an ellipse.

stem.setFrame(ew, eh-42, 40.0, 40.0);
st1 = new Area(stem);
stem.setFrame(ew+3, eh-47, 50.0, 50.0);
st2 = new Area(stem);
st1.subtract(st2);
g.fill(st1);

g.setColor(Color.yellow);

// Creates the pear itself by filling the Area resulting from the union of two Area objects created by two different ellipses.

circle.setFrame(ew-25, eh, 50.0, 50.0);
oval.setFrame(ew-19, eh-20, 40.0, 70.0);
circ = new Area(circle);
ov = new Area(oval);
circ.add(ov);
g.fill(circ);

g.draw(circ);

</twod:canvas>

and lo' and behold it created a Pear (and a mighty fine pear it is too! Sorry I couldn't resist the Benny Hill style innuendo).

I was most impressed and am already thinking about how I might use this to produce a Brilliant Button Maker style application. Another impressive thing is that, this method is virtually a scriptlet and yet I loved it! I started thinking that there must be other ways to create innovative and more generic non-JSP scriptlets inside JSP pages and there is, BSF.

Scripting in JSP, XSLT and BSF

BSF is one of those products that I have seen mentioned before but I have never previously taken the time to find out what exactly it does. It turns out it enables the incorporation of script languages in a JSP (amongst other things). Using BSF I can execute Python, TCL or Javascript (and other scripting languages) directly in my JSP.

Immediately I thought how cool it would be to make use of Rhino's support for E4X in my JSP pages but unfortunately it seems that BSF does not yet work with the latest version of Rhino. Nevertheless it looks like it could be a useful resource. Then I noticed that Xalan supports BSF for scripting. This means I can write an XSLT which, for example, makes use of external JavaScript function. Although this is interesting this seems a little too much like what Microsoft are doing with HTML Applications. Something about the concept of writing a complex Xalan specific application just seems a little standards ignorant and wrong to me.

3 comments:

Mark McLaren said...

See also Graphical taglibs in Coldtags suite: http://www.servletsuite.com/jsp.htm
Note: Comment imported. Original by Dan Novik website: http://www.servletsuite.com/jsp.htm at 2005-11-02 13:16

Mark McLaren said...

I have tried this example but it doesn't work. The tomcat

server (5.5) on my Linux machine says:



X11 connection rejected because of

wrong authentication.



The demo jsp-page is empty or the web browser (FireFox) can no more

connect to the server (I must restart tomcat). What is wrong? Many

thanks in advance,



...Rolf


Note: Comment imported. Original by Rolf at 2006-01-09 14:10

Mark McLaren said...

Hi Rolf,



Apparently this is a known problem with AWT on Unix systems.





If you are using Java 1.4+ you can fix this by setting

-Djava.awt.headless=true in your Tomcat starting configuration,



See http://www.jguru.com/faq/view.jsp?EID=222296


Note: Comment imported. Original by markmc website: http://cse-mjmcl.cse.bris.ac.uk/blog at 2006-01-09 16:42