Java Tip 140: Automatically generate JavaBeans from XSL files in J2EE applications
Use an XSL parameter parser to convert HTML parameter names to JavaBeans
By Klaus P. Berg, JavaWorld.com, 07/11/03
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
In the Java 2 Platform, Enterprise Edition (J2EE) world, servlets complement applets on the server side and complete the definition
of Java's role in the client/server hierarchy and in multitiered applications. Nowadays, J2EE plays a prominent role in Java's
acceptance, and numerous "Java-completing" techniques exist like, XML, XSLT (Extensible Stylesheet Language Transformations),
and Java object-to-XML conversion. Many available resources can help you learn about servlets, XML/XSLT, and HTML (see
Resources). Therefore, I do not discuss those techniques here, but focus on a problem that plays an important role in the everyday
life of a Web application developer who uses such techniques: writing Java statements that extract parameter values delivered
by HTML form elements and produce corresponding JavaBeans as intermediate data storage. These beans manage the data flow between
an application client (browser) and components running on the J2EE server, or between server components and a database. They
hold either the data the user entered in HTML forms or, more generally, any attribute passed either on the URL or in a POST.
For instance, a bean for a login page could have two properties: login and password. During request handling, such a bean
converts to an XML tree.
Figure 1 shows the overall architecture of a J2EE application based on Sun Microsystems' J2EE pattern recommendations and
blueprints (see Resources). StrutsCX, an open source framework for building enterprise applications, also uses a similar architecture.

Figure 1. J2EE application architecture using Java, XML, HTML, and XSLT. Click on thumbnail to view full-size image.
As a first step, the user typically fills out an input form and sends the data with a HTTP GET or POST request to the server.
The Front Controller servlet (Front Controller is a Sun J2EE pattern) examines the request and instantiates one or more helper
classes. A helper class's important tasks include reading the request parameters the user sends, validating them if necessary,
and storing them in a JavaBean. So, every page presented to the user corresponds to a JavaBean that serves as the model for
the HTML view in the well-known Model-View-Controller (MVC) pattern.
After the bean has been filled with the user form data, it is translated into XML (e.g., using Castor, an open source project
that transforms Java objects to XML). This XML stream is the input for an XSL transformation to produce the HTML output. XSLT
is an official World Wide Web Consortium (W3C) standard for a flexible and powerful language that transforms the structure
of XML data into text, PDF, HTML/XHTML (Extensible HTML), Wireless Markup Language (WML), VoiceXML, or any other XML format.
An XSLT processor like Xalan or Saxon actually performs the transformation using an XSL stylesheet, which itself is an XML
document (see Figure 2). You define rules for the XML data transformation inside the XSL stylesheet. The XSLT processor uses
these rules during transformation. In this article's context, the transformation is from XML to HTML.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download this article's associated source code, including XslBeanGenerator and XslParameterParser
http://www.javaworld.com/javaworld/javatips/javatip140/jw-javatip140.zip
- O'Reilly ParameterParser
http://www.servlets.com/cos/javadoc/com/oreilly/servlet/ParameterParser.html
- Open source StrutsCX framework
http://sourceforge.net/projects/strutscx
- Download Tomcat
http://jakarta.apache.org/tomcat/
- Download Sun's servlet package
http://java.sun.com/products/servlet/2.1/index.html
- Castor homepage
http://www.castor.org/
- Download the Xalan XSLT processor, which was used for all of this article's examples
http://xml.apache.org/xalan-j/index.html
- The current XSLT, XPath (XML Path Language), and XSL specifications can be found here
http://www.w3c.org/Style/XSL/
- Download Jakarta regular expressions
http://jakarta.apache.org/regexp/
- Core J2EE PatternsBest Practices and Design Strategies, Second Edition, Deepak Alur, John Crupi, Dan Malks (Prentice Hall, 2001; ISBN0131422464)
http://www.amazon.com/exec/obidos/tg/detail/-/0131422464/javaworld
- Sun Microsystems' J2EE BluePrints (click on The Sample Application and then on The Controller)
http://java.sun.com/blueprints/guidelines/designing_enterprise_applications/index.html
- "Boost Struts with XSLT and XML," Julien Mercay and Gilbert Bouzeid (JavaWorld, February 2002)
http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-strutsxslt.html
- "XML for the Absolute Beginner," Mark Johnson (JavaWorld, April 1999)
http://www.javaworld.com/javaworld/jw-04-1999/jw-04-xml.html
- "Use XML Data Binding to Do Your Laundry," Sam Brodkin (JavaWorld, December 2001)
http://www.javaworld.com/javaworld/jw-12-2001/jw-1228-jaxb.html
- "XSL Gives Your XML Some Style," Michael Ball (JavaWorld, June 2000)
http://www.javaworld.com/javaworld/jw-06-2000/jw-0630-xsl.html
- Read more XML-related articles in the Java and XML section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-xml-index.shtml
- View all previous Java Tips and submit your own
http://www.javaworld.com/columns/jw-tips-index.shtml
- Browse the JavaBeans section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-javabeans-index.shtml
- Browse the Java 2 Platform, Enterprise Edition (J2EE) section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-j2ee-index.shtml
- Visit the JavaWorld Forum
http://www.javaworld.com/javaforums/ubbthreads.php?Cat=&C=2
- Sign up for JavaWorld's free weekly email newsletters
http://www.javaworld.com/subscribe
Archived Discussions (Read only)