Using XSLT with Java Servlets

Using XSLT with Java Servlets

You can also use Java servlets to perform XSLT transformations on servers. Although many XSLT processors provide their own versions of servlets, Ive found it much easier to simply use Xalan or other XSLT processors to perform the transformation myself and serve the result document back to the client.

In the following example, I use Xalan to transform planets.xml into the temporary file planets.html using planets.xsl in a servlet:

 import java.net.*;  import java.sql.*;  import java.awt.*;  import java.awt.event.*;  import java.io.*;  import javax.servlet.*;  import org.apache.xalan.xslt.*;  public class xslservlet extends GenericServlet  {     public void service(ServletRequest request, ServletResponse response)          throws ServletException, IOException      {         try          {             XSLTProcessor processor = XSLTProcessorFactory.getProcessor();              processor.process(new XSLTInputSource("planets.xml"),                  new XSLTInputSource("planets.xsl"),                  new XSLTResultTarget("planets.html"));          }          catch(Exception e) {}         .         .         . 

All that remains is to send the HTML document back to the client, which looks like this:

Listing 10.14 Using Server-Based XSLT with Java Servlets
 import java.net.*;  import java.sql.*;  import java.awt.*;  import java.awt.event.*;  import java.io.*;  import javax.servlet.*;  import org.apache.xalan.xslt.*;  public class xslservlet extends GenericServlet  {     public void service(ServletRequest request, ServletResponse response)          throws ServletException, IOException      {         response.setContentType("text/html");          PrintWriter pw = response.getWriter();          try          {             XSLTProcessor processor = XSLTProcessorFactory.getProcessor();              processor.process(new XSLTInputSource("planets.xml"),                  new XSLTInputSource("planets.xsl"),                  new XSLTResultTarget("planets.html"));          }          catch(Exception e) {}          FileReader filereader = new FileReader("planets.html");          BufferedReader bufferedreader = new BufferedReader(filereader);          String instring;          while((instring = bufferedreader.readLine()) != null) {             pw.println(instring);          }          filereader.close();          pw.close();    }  } 

Thats all there is to it; you can see the result in Figure 10.7, which shows the HTML result as served from a servlet.

Figure 10.7. Using XSLT with Java servlets.
graphics/10fig07.gif

Thats it for this chapter on using XSLT processor APIs in programming code. Youve seen quite a bit here: how to interface to XSLT processor APIs using JavaScript and Java for the MSXML, Xalan, XT, Saxon, and Oracle XSLT processors, as well as examples of how to perform XSLT transformations on Web servers using ASP, JSP, and Java servlets.

The next chapter turns to what XSLT was originally created forusing it with XSL-FO.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net