Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 140: Automatically generate JavaBeans from XSL files in J2EE applications

Use an XSL parameter parser to convert HTML parameter names to JavaBeans

  • 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
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources