Java Tutorial - Java Scipt : Servlet Example

Java Tutorial - Java Scipt :

Servlet Example


This time we’re going to integrate the XML toolset into the Web layer. The Apache FOP tool is commonly used in J2EE applications to present data in .pdf format, so that’s the example we’ll tackle. We’ll build a servlet that takes in the FO document as a parameter and transforms that XML document into a .pdf file. Our servlet code is actually quite simple. All the heavy lifting is done for us by the Apache FOP library. In order to test this, you’ll need to have Tomcat running. If you haven’t set this up already, please refer to Chapter 5. Here’s the source code for SimpleFopServlet.java:

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.*;
import org.apache.fop.apps.*;
public class SimpleFopServlet extends HttpServlet
{
public void doGet(
HttpServletRequest oRequest,
HttpServletResponse oResponse )
throws IOException, ServletException
{
try {
String sSource = oRequest.getParameter( “src” );
oResponse.setContentType( “application/pdf” );
Driver oDriver = new Driver(
new InputSource( sSource ),
oResponse.getOutputStream() );
oDriver.setRenderer( Driver.RENDER_PDF );
oDriver.run();
}
catch( Exception e ) {
e.printStackTrace();
throw new ServletException( e.getMessage() );
}
}
}

We need to make the FOP libraries available for use by the servlet. The best way to package a library .jar for use with a servlet is within the WEB-INF/lib directory of the .war file. These .jar files will typically be loaded by the Web application server in such a manner as to make them accessible only to the files packaged in that .war.

Apache FOP is broken out into several different .jar files. The primary .jar file is fop.jar, but it has several dependencies on other projects. These .jar files are included in the distribution, and we need to provide these .jar files even though we won’t be directly calling upon them in the SimpleFopServlet. The .jar files needed to use FOP are:

·         avalon-framework-???.jar FOP relies on the Avalon common framework.

 The name of the avalon framework jar file will vary depending on the version of FOP you’re using. The name that is current with the writing of this book is avalon-framework-cvs-20020806.jar.

·         batik.jar. Provides the SVG capabilities for the FOP project.
·         fop.jar. The actual jar containing the Apache FOP classes.

Be sure to copy these jar files into WEB-INF\lib. The directory structure for the .war file will resemble the following:

/WEB-INF/web.xml
/WEB-INF/classes/SimpleFopServlet.class
/WEB-INF/lib/avalon-framework-???.jar
/WEB-INF/lib/batik.jar
/WEB-INF/lib/fop.jar
Here’s the source for web.xml:
<?xml version=”1.0” encoding=”ISO-8859-1”?>
<!DOCTYPE web-app PUBLIC
“-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN”
“http://java.sun.com/j2ee/dtds/web-app_2_2.dtd”>
<web-app>
<servlet>
<servlet-name>SimpleFop</servlet-name>
<servlet-class>SimpleFopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleFop</servlet-name>
<url-pattern>/simpleFop</url-pattern>
</servlet-mapping>
</web-app>

We’ll need to compile our servlet and package the compiled classes, the Web application deployment descriptor file, and library .jar files all into one .war file for deployment. Copy the Apache FOP jar and its dependencies into the WEB-INF\lib directory. The .jar files we’ll need are:

·         The Apache FOP .jar (fop.jar)
·         The Java Servlet .jar (servlet.jar)

On Windows, the process is:

javac -classpath .;.\WEBINF\ lib\fop.jar;%TOMCAT_HOME%\common\lib\servlet.jar SimpleFopServlet.java copy SimpleFopServlet.class WEB-INF\classes jar cvf fop-servlet.war WEB-INF

On Linux, the process is as follows:

javac -classpath .:./WEBINF/ lib/fop.jar:$TOMCAT_HOME/common/lib/servlet.jar SimpleFopServlet.java cp SimpleFopServlet.class WEB-INF/classes jar cvf fop-servlet.war WEB-INF

The compiled class files are placed under the WEB-INF/classes directory. In this case, we only have one servlet class file for SimpleFopServlet. This servlet is mapped to the URL pattern /simpleFop. The only parameter recognized by the servlet is src, which should contain the URL to the source FO document. Here’s a sample FO document (simple.fo) for testin :

<?xml version=”1.0” encoding=”utf-8”?>
<fo:root xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
<!-- Defines a template for content layouts called simple -->
<fo:layout-master-set>
<fo:simple-page-master master-name=”simple”
page-height=”8.5in”
page-width=”11.0in”
margin-top=”1.0in”
margin-bottom=”1.0in”
margin-left=”1.0in”
margin-right=”1.0in”>
<fo:region-body margin-top=”0.75in”
margin-bottom=”0.5in” />
</fo:simple-page-master>
</fo:layout-master-set>
<!-- Actual content page. -->
<fo:page-sequence master-reference=”simple” >
<fo:flow flow-name=”xsl-region-body”>
<fo:block text-align=”center” language=”en”>
Hello world!
</fo:block>
</fo:flow>
</fo:page-sequence>
<fo:page-sequence master-reference=”simple” >
<fo:flow flow-name=”xsl-region-body”>
<fo:block text-align=”justify” language=”en”>
Page 2. More content here.
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>

To try out the servlet, deploy the .war file to your Web application server. Once you have deployed the code on Tomcat, the URL used to invoke the FOP processing via a servlet is: http://localhost:8080/fop servlet/simpleFop?src=file:///C:/temp/simple.fo Naturally, this depends on how Tomcat is set up on your system. The first part of the URL, http:// localhost:8080, specifies the server and port for our Tomcat installation. /fop servlet is the subtree where the .war file is bound by default. This is followed by the URL mapping we specified in the

web.xml file, simpleFop. Finally we have the portion of the URL containing the src parameter. The result can be seen in Figure 12.1. As a reporting tool, the servlet/FOP combination is great. Not only can you generate .pdf and other printable formats, but it is also possible to create output SVG images. With an additional transformation step, SVG can be rendered into PNG, GIF, or JPEG. The packaging is similar to the EJB integration example, but servlets are nice because they can be directly accessed by HTTP. You’ll find that servlets are one of the best and most common places to integrate XML.