Ditchnet JSP 2D Taglib

A polygon

Overview

With the JSP 2D Taglib, your web applications now have full access to the Java 2D API for dynamically rendering beautiful graphics primitives (like the shape to the right) directly in your web page without limitation. Yes, the web page itself becomes a canvas on which you can leverage the full power of the Java 2D API for graphics. Literally all browsers are supported... no Applets, JavaScript, Ajax, Flash, Plug-Ins, Cookies, Lazlo or any other hackery required on the client! (We keep our hackery on the server, thank you.)

The 2D taglib is similar in concept to the <canvas> tag pioneered by Apple in Safari 2 and later accepted by the WHATWG in one of their specs... it allows web page authors to script graphics primitives directly in the HTML source of a page using JavaScript (although the user need not have JavaScript enabled in her browser). For example, the rounded red pentagon to the right was authored with this tag:

<twod:canvas id="polygon" width="200" height="200" alt="A polygon"> // create red gradient paint var color1 = new java.awt.Color(0xC80021); var color2 = new java.awt.Color(0x711928); var x1 = 20; var y1 = 0; var x2 = 20; var y2 = 190; var grad = new java.awt.GradientPaint(x1,y1,color1,x2,y2,color2); // create pentagon var xPoints = [101,180,150, 52, 20]; var yPoints = [24 , 81,177,177, 81]; var numPoints = xPoints.length; var pentagon = new java.awt.Polygon(xPoints,yPoints,numPoints); // create stroke with rounded corners var endCap = java.awt.BasicStroke.CAP_ROUND; var join = java.awt.BasicStroke.JOIN_ROUND; var stroke = new java.awt.BasicStroke(20,endCap,join); // draw polygon on the current graphics context (g) g.setPaint(grad); g.fill(pentagon); g.setStroke(stroke); g.draw(pentagon); </twod:canvas>

Wow! As you can see, the entire Java 2D API is exposed to the web author inside a <twod:canvas> tag via JavaScript. Web page authors can use all of the nice scripting features of the familiar JavaScript language (native collection types like Arrays and Object dictionaries, and dynamic typing) to harness the massively powerful Java 2D API -- and the result renders directly in the web page! The 2D Taglib is capable of rendering beautiful, high-quality, anti-aliased graphics and text.

Additionally, each <twod:canvas> tag is implicitly provided a java.awt.Graphics2D instance as a globally-scoped JavaScript variable named g. This is the graphics context for the current canvas, and the graphical effects you invoke on it will appear in an image on the web page.

But wait, there's more! Just like any JSP or JSP EL expression, the <twod:canvas> tag has access to implicit read-only JavaScript Object represenations of the applicationScope, sessionScope, requestScope, pageScope, request parameters and more! This is where the true power of the 2D Taglib becomes evident...

Imagine for a moment that you work for a web hosting company that has developed a web app to edit your client's server configurations... Objects representing those servers are stored throughout the life of the application in each of the scopes mentioned above. At runtime, using the 2D Taglib, your application could access the server objects in a given scope and render dynamic graphical representations of a given client's hardware configuration. Look ma, no Photoshop!

Source code and binaries of version 0.1 of the JSP 2D Taglib are available under the LGPL.

Next Tab » WTF?

Valid XHTML 1.0!