Here's a simple servlet that will work.  Like the FOP servlet, you give it a
fo file or an xml/xsl combination in a get parameter.  You have to have the
latest xalan and xerces in addition to fop, because I handle the xml
transformation within the servlet as opposed to using the fop methods for
transformation.  Let me know if you have any trouble.

Mike Z

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.InputSource;
import org.apache.fop.apps.Driver;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;

public class SimpleServlet extends HttpServlet {

    private static final String FO_REQUEST_PARAM = "fo";
    private static final String XML_REQUEST_PARAM = "xml";
    private static final String XSL_REQUEST_PARAM = "xsl";

    // you might want to override init() and set up a log4j category for the
servlet so you can easily add debugging
    // code

    public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

       try {
                   // you might want to set up a constant to represent the
directory path or pull the directory path
                   // from a configuration file, otherwise you have to
supply the path in the get parameter
            String foParam = request.getParameter(FO_REQUEST_PARAM);
            String xmlParam = request.getParameter(XML_REQUEST_PARAM);
            String xslParam = request.getParameter(XSL_REQUEST_PARAM);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputSource input = null;

            // set content type as pdf so browser will know it's getting pdf
content and display it properly
            response.setContentType("application/pdf");

            if ((request.getParameter(FO_REQUEST_PARAM)) != null) {
                FileInputStream fo = new FileInputStream(foParam);
                input = new InputSource(fo);
            } else if(((request.getParameter(XML_REQUEST_PARAM)) != null) &&
((request.getParameter(XSL_REQUEST_PARAM)) != null)) {
                File xml = new File(xmlParam);
                File xsl = new File(xslParam);
                Source xmlSource = new StreamSource(xml);
                                Source xslSource = new StreamSource(xsl);
                                StringWriter sw = new StringWriter();
                                TransformerFactory tFactory =
TransformerFactory.newInstance();
                                //create Templates so the xsl document will
function as a template and will be cached in
                                //order to optimize performance
                                Templates cachedXSLT =
tFactory.newTemplates(xslSource);
                                Transformer transformer =
cachedXSLT.newTransformer();
                                //transform the xml we've been given using
the cached xsl
                                transformer.transform(xmlSource, new
StreamResult(sw));
                                Reader r = new StringReader(sw.toString());
                                input = new InputSource(r);
            } else {
                                // handle situation:  get parameters are not
correct
            }
                        if (input != null) {
                                Driver driver = new Driver(input, out);
                                driver.setRenderer(Driver.RENDER_PDF);
                                driver.run();
                        }

            //send content to the browser now
            byte[] content = out.toByteArray();
                        response.setContentLength(content.length);
                        response.getOutputStream().write(content);
                        response.getOutputStream().flush();
        } catch (Exception e) {
            // handle exceptions.
        }
    }

        // pass post data received to get for processing
    public void doPost(HttpServletRequest request,
                                                  HttpServletResponse
response)
                        throws IOException, ServletException
                {
                        doGet(request, response);
        }
}

-----Original Message-----
From: Chinn, Gale [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 05, 2002 7:58 AM
To: '[EMAIL PROTECTED]'
Subject: Help to get started in a servlet



I am new to fop and can get it to run command line, but would like to get it
to run in a servlet.
I have followed the info on fop's embedding page using fop.war file in the
webapps directory of Tomcat on ver 3.2 and 4, but cannot get it to work
using:
http://localhost:8080/fop/fop?fo=... or
http://localhost:8080/fop/servlet/fop?fo=... 

I would just like to get a simple pdf to come back to my browser to prove a
point to my group leader.  Any help would be appreciated

TIA,

Gale Chinn
Programmer/Analyst
Cessna Aircraft Company
Wichita, KS

Reply via email to