J2EE Course: XML and XSLT Programming With JAXP

This free online course will teach you how to use Java to programmatically work with XML in a web application. Please complete the XML and XSLT Overview Course before taking this course, which will provide you with an introduction to XML and XSLT.

Course Prep:

  1. Read chapter 2 in the J2EE and XML Development textbook from Manning Publishing Co (free chapter).
  2. View the Java XML and XSLT lecture (also available in pdf format here).

The exercises in this course build on the web application created in the J2EE Course - Building Servlet Web Applications course. Please complete that course first before starting the XML and XSLT Programming With JAXP course.

Course Exercises:

  1. Open the Eclipse Tomcat project created in the J2EE Course - Building Servlet Web Applications course and add a Java class called AccountBean.java. Create a getAccountInfo(String User, String Password) method that builds an XML String in the format shown below.

    Note: Use JAXP methods such as createElement and appendChild - do not use String concatenation (there's nothing wrong with String concatenation, but you won't learn to use JAXP if you rely on String concatenation). Replace the user and password values with the values the user enters from the login.html page.

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="accounts.xslt"?>
    <accounts user="Jeff" password="astro">
        <account id="153603" name=" Checking" balance="2593.41"/>
        <account id="695486" name="Savings" balance="46329.60"/>
    </accounts>

  2. Create an XSLT stylesheet called accounts.xslt that converts the XML above into HTML into the format shown below. The accounts.xslt stylesheet should be created in the WEB-INF folder of your web project.

    <html>
    <body>
    <p>Welcome Jeff,</p>
    <p>Your account information is listed below: </p>
        <table border="0" width="100%">
            <tr><th>Account ID</th><th> Account Name</th><th>Balance</th></tr>
            <tr><td>153603</td><td>Checking</td><td>$2593.41</td></tr>
           
    <tr><td>695486</td><td>Savings</td><td>$46329.60</td></tr>
        </table>
    </body>
    </html>

  3. Create a Java class called XMLUtil.java with a transformXML(Document DocXML, InputStream XSLTFile) method. This method should transform an XML Document into an HTML String using the specified XSLT file. The getServletContext().getResourceAsStream("/WEB-INF/accounts.xslt") method can be used inside your LoginServlet to get an InputStream that points to the accounts.xslt stylesheet located in the WEB-INF folder in your web project.
  4. Load an AccountBean object from the LoginServlet's doPost method, and pass the username and password parameters submitted from login.html to the getAccountInfo method.
  5. Load an XMLUtil object from LoginServlet, and pass the XML Document to XMLUtil to convert it to HTML. Return the transformed HTML String to the LoginServlet’s response, so that it displays in the browser. The web page displayed in the browser should look like this:

    Welcome Jeff,

    Your account information is listed below:

    Account ID Account Name Balance
    153603 Checking $2593.41
    695486 Savings $46329.60

  6. Package the web application into a war file and deploy to Tomcat's webapps folder.
  7. Run your application by entering http://localhost/YourWebAppName.war in your browser (or http://localhost:8080/YourWebAppName.war if your Tomcat server is running on port 8080).

Check your work: assign05_Solution.war

Tips and Tricks

This assignment is best taken in steps that build on each other. I recommend that you first manually create the xml and xslt files, and validate the transformed result (which will be the html displayed in the browser). This will ensure that your stylesheet is accurate. Then you can build a Java application that will dynamically create the xml document in memory and apply the transformation programmatically.

Once these steps have been taken, then the transformation logic can be moved into the web application. All of the transformation code can be copied directly from the powerpoint.

When you create xml elements and attributes programmatically using Java (e.g. createElement and setAttribute methods), be sure to call appendChild to attach the root element to the xml Document. You'll need to append the root "accounts" element to the xml Document, and then append each child "accounts" element to the "account" parent element.


Fix-It Utilities 7 Professional Download

Fix-It Utilities 7 Professional Download

PC Diagnostics & Utilities that keep your system running better, faster, and longer! Fix-It Utilities 7 now comes with 3 user licenses!Why PC Utilities? Like a car, PCs need maintenance and repair. Running PC diagnostics and utilities on your computer is the equivalent of taking your car in for a tune-up. Fix-It Utilities 7 Professional optimizes system performance and our computer diagnostics finds and prevents problems before they occur. In addition, have easy, scheduled maintenance to keep your PC in top shape. Fix-It Utilities keeps your computer running like new.

Special Offer: Save 50% with Coupon Code FIXITBLOWOUT