Browse DevX
Sign up for e-mail newsletters from DevX


Programmatically Apply XSLT in a Dynamic Java Application-4 : Page 4




Building the Right Environment to Support AI, Machine Learning and Deep Learning

XSLT and J2EE: A Practical Example
Now let's take a look at a practical J2EE example. You'll need a JAXP-compliant XML parser (basic XML parsing) and a JAXP-compliant XML processor (for XSLT processing). You also will need a Web server with a J2EE servlet container. If you do not already have these, download the Java Web Services Developers Pack (JWSDP) from Sun Microsystems' Web site. In addition to other tools and APIs, the JWSDP contains the JAXP API, an XML parser (Xerces), an XML processor (Xalan), and a Web server with a J2EE servlet container (Tomcat 4.0).

If you are using the JWSDP, be sure that you have the following environment variables set:

Variable Name Variable Value
JAVA_HOME The root of your JDK directory
JWSDP_HOME The root of your JWSDP distribution
PATH Bin directories for J2SE and JWSDP should appear first in this list.

For your J2EE application, you will write a simple servlet. Your servlet will accept an HTTP parameter that indicates the XML file to display (via the XSLT stylesheet). It then will locate the file and associated stylesheet, apply the transformation, and push the results back to the client.

Writing the app begins with a series of imports and the basic servlet class declaration:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class XSLTServlet extends HttpServlet {

Next, instantiate a TransformerFactory object and create a String to represent the directory where the XML and XSL stylesheets can be located.

  TransformerFactory tFactory = 
TransformerFactory.newInstance(); String directory =

Define the remainder of the servlet in this doGet() method:

  public void doGet( HttpServletRequest req, 
HttpServletResponse res ) throws IOException, ServletException { String page = directory + req.getParameter("page" ); if( page != null ) { try { Transformer trans =
tFactory.newTransformer( new StreamSource( page + ".xsl" ) ); trans.transform(
new StreamSource( page + ".xml" ), new StreamResult( res.getOutputStream() ) );
} catch( TransformerException te )
{ te.printStackTrace(); } } else { PrintWriter out = res.getWriter(); out.println("Page parameter missing." ); }//end if( page != null ) }//end doGet() }//end class XSLTServlet

The value of the page parameter is retrieved from the request and used in conjunction with the directory variable defined earlier to construct the path to the XML files. Once the transformation takes place, the result is funneled into the response object's output stream.

Now all you do is deploy this servlet within a new or existing Web application and start up Tomcat (via the startup script contained in the bin directory). Modify the directory name that is hard-coded as the value for the directory variable so that it reflects the name and location of this Web application. Place an XML file and the corresponding XSL file into the WEB-INF directory for the application (they should have identical names but different extensions).

Finally, punch "http://localhost:8080/WEB-APP-NAME/servlet/XSLTServlet?page=Weather" into your Web browser to access your application (substitute the word Weather for whatever you named your XML and XSL files). The application searches the designated directory for two files, one named Weather.xml and the other named Weather.xsl. It then applies the transformations defined in the stylesheet to the data in the XML file and pushes the results onto the output stream buffer.

Now that you understand how the XSLT mechanism works, you see how an entire J2EE Web site's content could be described and transformed for display using XML.

DevX Java Pro Kyle Gabhart is an independent consultant, trainer, and public speaker specializing in Java technologies, XML, and Web services technologies. Visit his Web site http://www.gabhart.com to view his other writings and his upcoming speaking engagements.
Thanks for your registration, follow us on our social networks to keep up-to-date