Travel project. We used com.ibm.itso package. WAS_V4_XALAN variable is required in the Java Build Path to create this servlet.

Example 9-5 GetPassengerList Server doGet method

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException {

String xslFile = "/WEB-INF/xsl/passengerList.xsl"; String xmlFile = "/WEB-INF/passengerList.xml"; try

{

resp.setContentType("text/html"); PrintWriter out = resp.getWriter();

TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(

new StreamSource( getServletContext().getResource(xslFile).toExternalForm()));

StreamSource source = new StreamSource( getServletContext().getResource(xmlFile).toExternalForm());

transformer.transform(source,new StreamResult(out));

}

catch(Exception ex)

{

System.out.println("Error in doGet "+ex.toString());

}

}

The ServletContext provides own getResource method. It returns the resource located on the location of the current Web application. To load files from the current WAR file, you need to specify the file name, start with /WEB-INF/. In this case you need to copy the following files into the /WEB-INF/ directory:

￿passengerList.xml (sample data)

￿PassengerList.xsd (required by passengerList.xml)

￿passengerList.xsl (create /WEB-INF/xsl directory and put it inside)

Using JAXP

As we mentioned about JAXP, it provides a standard Java interface to many XSLT processors. We used three JAXP classes in our servlet.

￿javax.xml.transform.Source

This interface is used to read XML and XSLT file. This can read from a stream type object such as an InputStream, or a Reader.

190 The XML Files: Development of XML/XSL Applications Using WebSphere Studio

Page 206
Image 206
IBM Version 5 manual Javax.xml.transform.Source, Example 9-5 GetPassengerList Server doGet method