You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

servlets.xml 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?xml version="1.0" standalone="no"?>
  2. <!--
  3. Copyright 1999-2006 The Apache Software Foundation
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. -->
  14. <!-- $Id$ -->
  15. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
  16. <document>
  17. <header>
  18. <title>Servlets</title>
  19. <subtitle>How to use Apache FOP in a Servlet</subtitle>
  20. <version>$Revision$</version>
  21. </header>
  22. <body>
  23. <section id="overview">
  24. <title>Overview</title>
  25. <p>
  26. This page discusses topic all around using Apache FOP in a servlet environment.
  27. </p>
  28. </section>
  29. <section id="example-servlets">
  30. <title>Example Servlets in the FOP distribution</title>
  31. <p>
  32. In the directory {fop-dir}/src/java/org/apache/fop/servlet, you'll find a working example
  33. of a FOP-enabled servlet.
  34. </p>
  35. <p>
  36. The servlet is automatically built when you build Apache FOP using the supplied Ant script. After building
  37. the servlet, drop fop.war into the webapps directory of Apache Tomcat (or any other web container). Then, you can use
  38. URLs like the following to generate PDF files:
  39. </p>
  40. <ul>
  41. <li>http://localhost:8080/fop/fop?fo=/home/path/to/fofile.fo</li>
  42. <li>http://localhost:8080/fop/fop?xml=/home/path/to/xmlfile.xml&amp;xsl=/home/path/to/xslfile.xsl</li>
  43. </ul>
  44. <p/>
  45. <p>The source code for the servlet can be found under {fop-dir}/src/java/org/apache/fop/servlet/FopServlet.java.</p>
  46. <note>
  47. This example servlet should not be used on a public web server connected to the Internet as it does not contain
  48. any measures to prevent Denial-of-Service-Attacks. It is provided as an example and as a starting point for
  49. your own servlet.
  50. </note>
  51. </section>
  52. <section id="servlet">
  53. <title>Create your own Servlet</title>
  54. <note>
  55. This section assumes you are familiar with <a href="embedding.html">embedding FOP</a>.
  56. </note>
  57. <section id="minimal-servlet">
  58. <title>A minimal Servlet</title>
  59. <p>
  60. Here is a minimal code snippet to demonstrate the basics:
  61. </p>
  62. <source>private FopFactory fopFactory = FopFactory.newInstance();
  63. private TransformerFactory tFactory = TransformerFactory.newInstance();
  64. public void doGet(HttpServletRequest request,
  65. HttpServletResponse response) throws ServletException {
  66. try {
  67. response.setContentType("application/pdf");
  68. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream());
  69. Transformer transformer = tFactory.newTransformer();
  70. Source src = new StreamSource("foo.fo");
  71. Result res = new SAXResult(fop.getDefaultHandler());
  72. transformer.transform(src, res);
  73. } catch (Exception ex) {
  74. throw new ServletException(ex);
  75. }
  76. }</source>
  77. <note>
  78. There are numerous problems with the code snippet above.
  79. Its purpose is only to demonstrate the basic concepts.
  80. See below for details.
  81. </note>
  82. </section>
  83. <section id="xslt">
  84. <title>Adding XSL tranformation (XSLT)</title>
  85. <p>
  86. A common requirement is to transform an XML source to
  87. XSL-FO using an XSL transformation. It is recommended to use
  88. JAXP for this task. The following snippet shows the basic
  89. code:
  90. </p>
  91. <source>private FopFactory fopFactory = FopFactory.newInstance();
  92. private TransformerFactory tFactory = TransformerFactory.newInstance();
  93. public void init() throws ServletException {
  94. //Optionally customize the FopFactory and TransformerFactory here
  95. }
  96. [..]
  97. //Setup a buffer to obtain the content length
  98. ByteArrayOutputStream out = new ByteArrayOutputStream();
  99. //Setup FOP
  100. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
  101. //Setup Transformer
  102. Source xsltSrc = new StreamSource(new File("foo-xml2fo.xsl"));
  103. Transformer transformer = tFactory.newTransformer(xsltSrc);
  104. //Make sure the XSL transformation's result is piped through to FOP
  105. Result res = new SAXResult(fop.getDefaultHandler());
  106. //Setup input
  107. Source src = new StreamSource(new File("foo.xml"));
  108. //Start the transformation and rendering process
  109. transformer.transform(src, res);
  110. //Prepare response
  111. response.setContentType("application/pdf");
  112. response.setContentLength(out.size());
  113. //Send content to Browser
  114. response.getOutputStream().write(out.toByteArray());
  115. response.getOutputStream().flush();</source>
  116. <note>
  117. Buffering the generated PDF in a ByteArrayOutputStream is done to avoid potential
  118. problems with the Acrobat Reader Plug-in in Microsoft Internet Explorer.
  119. </note>
  120. <p>
  121. The <code>Source</code> instance used above is simply an
  122. example. If you have to read the XML from a string, supply
  123. a <code>new StreamSource(new
  124. StringReader(xmlstring))</code>. Constructing and reparsing
  125. an XML string is generally less desirable than using a
  126. SAXSource if you generate your XML. You can alternatively
  127. supply a DOMSource as well. You may also use dynamically
  128. generated XSL if you like.
  129. </p>
  130. <p>
  131. Because you have an explicit <code>Transformer</code> object, you can also use it to
  132. explicitely set parameters for the transformation run.
  133. </p>
  134. </section>
  135. <section id="cfg">
  136. <title>Custom configuration</title>
  137. <p>
  138. You can easily set up your own FOUserAgent as demonstrated on the <a href="embedding.html">Embedding page</a>.
  139. </p>
  140. </section>
  141. <section id="performance">
  142. <title>Improving performance</title>
  143. <p>
  144. There are several options to consider:
  145. </p>
  146. <ul>
  147. <li>
  148. Instead of java.io.ByteArrayOutputStream consider using the ByteArrayOutputStream
  149. implementation from the <a href="ext:jakarta/commons/io">Jakarta Commons IO project</a> which allocates less memory.
  150. The full class name is: <code>org.apache.commons.io.output.ByteArrayOutputStream</code>
  151. </li>
  152. <li>
  153. In certain cases it can help to write the generated PDF to a temporary file so
  154. you can quickly reuse the file. This is especially useful, if Internet Explorer
  155. calls the servlet multiple times with the same request or if you often generate
  156. equal PDFs.
  157. </li>
  158. </ul>
  159. <p>
  160. Of course, the
  161. <a href="embedding.html#performance">performance hints from the Embedding page</a>
  162. apply here, too.
  163. </p>
  164. </section>
  165. </section>
  166. <section id="ie">
  167. <title>Notes on Microsoft Internet Explorer</title>
  168. <p>
  169. Some versions of Internet Explorer will not automatically show the PDF or call the servlet multiple times.
  170. These are well-known limitations of Internet Explorer and are not a problem of the servlet.
  171. However, Internet Explorer can still be used to download the PDF so that it can be viewed later.
  172. Here are some suggestions in this context:
  173. </p>
  174. <ul>
  175. <li>
  176. Use an URL ending in <code>.pdf</code>, like
  177. <code>http://myserver/servlet/stuff.pdf</code>. Yes, the servlet can
  178. be configured to handle this. If the URL has to contain parameters,
  179. try to have <strong>both</strong> the base URL as well as the last parameter end in
  180. <code>.pdf</code>, if necessary append a dummy parameter, like
  181. <code>http://myserver/servlet/stuff.pdf?par1=a&amp;par2=b&amp;d=.pdf</code>. The
  182. effect may depend on IEx version.
  183. </li>
  184. <li>
  185. Give IEx the opportunity to cache. In particular, ensure the
  186. server does not set any headers causing IEx not to cache the
  187. content. This may be a real problem if the document is sent
  188. over HTTPS, because most IEx installations will by default
  189. <em>not</em> cache any content retrieved over HTTPS.
  190. Setting the <code>Expires</code> header entry may help in
  191. this case:<br/> <code>response.setDateHeader("Expires",
  192. System.currentTimeMillis() + cacheExpiringDuration *
  193. 1000);</code><br/> Consult your server manual and the
  194. relevant RFCs for further details on HTTP headers and
  195. caching.
  196. </li>
  197. <li>
  198. Cache in the server. It may help to include a parameter in
  199. the URL which has a timestamp as the value min order to
  200. decide whether a request is repeated. IEx is reported to
  201. retrieve a document up to three times, but never more often.
  202. </li>
  203. </ul>
  204. </section>
  205. <section id="servlet-engine">
  206. <title>Servlet Engines</title>
  207. <p>
  208. When using a servlet engine, there are potential CLASSPATH issues, and potential conflicts
  209. with existing XML/XSLT libraries. Servlet containers also often use their own classloaders
  210. for loading webapps, which can cause bugs and security problems.
  211. </p>
  212. <section id="tomcat">
  213. <title>Tomcat</title>
  214. <p>
  215. Check Tomcat's documentation for detailed instructions about installing FOP and Cocoon.
  216. There are known bugs that must be addressed, particularly for Tomcat 4.0.3.
  217. </p>
  218. </section>
  219. <section id="websphere">
  220. <title>WebSphere 3.5</title>
  221. <p>
  222. Put a copy of a working parser in some directory where WebSphere can access it.
  223. For example, if /usr/webapps/yourapp/servlets is the CLASSPATH for your servlets,
  224. copy the Xerces jar into it (any other directory would also be fine).
  225. Do not add the jar to the servlet CLASSPATH, but add it to the CLASSPATH of the
  226. application server which contains your web application.
  227. In the WebSphere administration console, click on the "environment" button in the
  228. "general" tab. In the "variable name" box, enter "CLASSPATH".
  229. In the "value" box, enter the correct path to the parser jar file
  230. (/usr/webapps/yourapp/servlets/Xerces.jar in our example here).
  231. Press "OK", then apply the change and restart the application server.
  232. </p>
  233. </section>
  234. </section>
  235. <section id="complex-usecases">
  236. <title>Handling complex use cases</title>
  237. <p>
  238. Sometimes the requirements for a servlet get quite sophisticated: SQL data sources,
  239. multiple XSL transformations, merging of several datasources etc. In such a case
  240. consider using <a class="fork" href="ext:cocoon">Apache Cocoon</a> instead
  241. of a custom servlet to accomplish your goal.
  242. </p>
  243. </section>
  244. </body>
  245. </document>