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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?xml version="1.0" standalone="no"?>
  2. <!--
  3. Copyright 1999-2005 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. </section>
  47. <section id="servlet">
  48. <title>Create your own Servlet</title>
  49. <note>
  50. This section assumes you are familiar with <a href="embedding.html">embedding FOP</a>.
  51. </note>
  52. <section id="minimal-servlet">
  53. <title>A minimal Servlet</title>
  54. <p>
  55. Here is a minimal code snippet to demonstrate the basics:
  56. </p>
  57. <source>public void doGet(HttpServletRequest request,
  58. HttpServletResponse response) throws ServletException {
  59. try {
  60. response.setContentType("application/pdf");
  61. Fop fop = new Fop(Constants.RENDER_PDF);
  62. fop.setOutputStream(response.getOutputStream());
  63. TransformerFactory factory = TransformerFactory.newInstance();
  64. Transformer transformer = factory.newTransformer();
  65. Source src = new StreamSource("foo.fo");
  66. Result res = new SAXResult(fop.getDefaultHandler());
  67. transformer.transform(src, res);
  68. } catch (Exception ex) {
  69. throw new ServletException(ex);
  70. }
  71. }</source>
  72. <note>
  73. There are numerous problems with the code snippet above.
  74. Its purpose is only to demonstrate the basic concepts.
  75. See below for details.
  76. </note>
  77. </section>
  78. <section id="xslt">
  79. <title>Adding XSL tranformation (XSLT)</title>
  80. <p>
  81. A common requirement is the to transform an XML source to
  82. XSL-FO using an XSL transformation. It is recommended to use
  83. JAXP for this task. The following snippet shows the basic
  84. code:
  85. </p>
  86. <source>
  87. protected TransformerFactory transformerFactory;
  88. public void init() throws ServletException {
  89. this.transformerFactory = TransformerFactory.newInstance();
  90. }
  91. [..]
  92. //Setup FOP
  93. Fop fop = new Fop(Constants.RENDER_PDF);
  94. //Setup a buffer to obtain the content length
  95. ByteArrayOutputStream out = new ByteArrayOutputStream();
  96. fop.setOutputStream(out);
  97. //Setup Transformer
  98. Source xsltSrc = new StreamSource(new File("foo-xml2fo.xsl"));
  99. Transformer transformer = this.transformerFactory.newTransformer(xsltSrc);
  100. //Make sure the XSL transformation's result is piped through to FOP
  101. Result res = new SAXResult(driver.getDefaultHandler());
  102. //Setup input
  103. Source src = new StreamSource(new File("foo.xml"));
  104. //Start the transformation and rendering process
  105. transformer.transform(src, res);
  106. //Prepare response
  107. response.setContentType("application/pdf");
  108. response.setContentLength(out.size());
  109. //Send content to Browser
  110. response.getOutputStream().write(out.toByteArray());
  111. response.getOutputStream().flush();</source>
  112. <note>
  113. Buffering the generated PDF in a ByteArrayOutputStream is done to avoid potential
  114. problems with the Acrobat Reader Plug-in in Microsoft Internet Explorer.
  115. </note>
  116. <p>
  117. The <code>Source</code> instance used above is simply an
  118. example. If you have to read the XML from a string, supply
  119. a <code>new StreamSource(new
  120. StringReader(xmlstring))</code>. Constructing and reparsing
  121. an XML string is generally less desirable than using a
  122. SAXSource if you generate your XML. You can alternatively
  123. supply a DOMSource as well. You may also use dynamically
  124. generated XSL if you like.
  125. </p>
  126. <p>
  127. Because you have an explicit <code>Transformer</code> object, you can also use it to
  128. explicitly set parameters for the transformation run.
  129. </p>
  130. </section>
  131. <section id="cfg">
  132. <title>Custom configuration</title>
  133. <p>
  134. You can easily set up your own FOUserAgent as demonstrated on the <a href="embedding.html">Embedding page</a>.
  135. </p>
  136. </section>
  137. <section id="performance">
  138. <title>Improving performance</title>
  139. <p>
  140. There are several options to consider:
  141. </p>
  142. <ul>
  143. <li>
  144. Instead of java.io.ByteArrayOutputStream consider using the ByteArrayOutputStream
  145. implementation from the <a href="ext:jakarta/commons/io">Jakarta Commons IO project</a> which allocates less memory.
  146. The full class name is: <code>org.apache.commons.io.output.ByteArrayOutputStream</code>
  147. </li>
  148. <li>
  149. In certain cases it can help to write the generated PDF to a temporary file so
  150. you can quickly reuse the file. This is especially useful, if Internet Explorer
  151. calls the servlet multiple times with the same request or if you often generate
  152. equal PDFs.
  153. </li>
  154. </ul>
  155. <p>
  156. Of course, the
  157. <a href="embedding.html#performance">performance hints from the Embedding page</a>
  158. apply here, too.
  159. </p>
  160. </section>
  161. </section>
  162. <section id="ie">
  163. <title>Notes on Microsoft Internet Explorer</title>
  164. <p>
  165. Some versions of Internet Explorer will not automatically show the PDF or call the servlet multiple times.
  166. These are well-known limitations of Internet Explorer and are not a problem of the servlet.
  167. However, Internet Explorer can still be used to download the PDF so that it can be viewed later.
  168. Here are some suggestions in this context:
  169. </p>
  170. <ul>
  171. <li>
  172. Use an URL ending in <code>.pdf</code>, like
  173. <code>http://myserver/servlet/stuff.pdf</code>. Yes, the servlet can
  174. be configured to handle this. If the URL has to contain parameters,
  175. try to have <strong>both</strong> the base URL as well as the last parameter end in
  176. <code>.pdf</code>, if necessary append a dummy parameter, like
  177. <code>http://myserver/servlet/stuff.pdf?par1=a&amp;par2=b&amp;d=.pdf</code>. The
  178. effect may depend on IEx version.
  179. </li>
  180. <li>
  181. Give IEx the opportunity to cache. In particular, ensure the
  182. server does not set any headers causing IEx not to cache the
  183. content. This may be a real problem if the document is sent
  184. over HTTPS, because most IEx installations will by default
  185. <em>not</em> cache any content retrieved over HTTPS.
  186. Setting the <code>Expires</code> header entry may help in
  187. this case:<br/> <code>response.setDateHeader("Expires",
  188. System.currentTimeMillis() + cacheExpiringDuration *
  189. 1000);</code><br/> Consult your server manual and the
  190. relevant RFCs for further details on HTTP headers and
  191. caching.
  192. </li>
  193. <li>
  194. Cache in the server. It may help to include a parameter in
  195. the URL which has a timestamp as the value min order to
  196. decide whether a request is repeated. IEx is reported to
  197. retrieve a document up to three times, but never more often.
  198. </li>
  199. </ul>
  200. </section>
  201. <section id="servlet-engine">
  202. <title>Servlet Engines</title>
  203. <p>
  204. When using a servlet engine, there are potential CLASSPATH issues, and potential conflicts
  205. with existing XML/XSLT libraries. Servlet containers also often use their own classloaders
  206. for loading webapps, which can cause bugs and security problems.
  207. </p>
  208. <section id="tomcat">
  209. <title>Tomcat</title>
  210. <p>
  211. Check Tomcat's documentation for detailed instructions about installing FOP and Cocoon.
  212. There are known bugs that must be addressed, particularly for Tomcat 4.0.3.
  213. </p>
  214. </section>
  215. <section id="websphere">
  216. <title>WebSphere 3.5</title>
  217. <p>
  218. Put a copy of a working parser in some directory where WebSphere can access it.
  219. For example, if /usr/webapps/yourapp/servlets is the CLASSPATH for your servlets,
  220. copy the Xerces jar into it (any other directory would also be fine).
  221. Do not add the jar to the servlet CLASSPATH, but add it to the CLASSPATH of the
  222. application server which contains your web application.
  223. In the WebSphere administration console, click on the "environment" button in the
  224. "general" tab. In the "variable name" box, enter "CLASSPATH".
  225. In the "value" box, enter the correct path to the parser jar file
  226. (/usr/webapps/yourapp/servlets/Xerces.jar in our example here).
  227. Press "OK", then apply the change and restart the application server.
  228. </p>
  229. </section>
  230. </section>
  231. <section id="complex-usecases">
  232. <title>Handling complex use cases</title>
  233. <p>
  234. Sometimes the requirements for a servlet get quite sophisticated: SQL data sources,
  235. multiple XSL transformations, merging of several datasources etc. In such a case
  236. consider using <a class="fork" href="ext:cocoon">Apache Cocoon</a> instead
  237. of a custom servlet to accomplish your goal.
  238. </p>
  239. </section>
  240. </body>
  241. </document>
  242. <!-- Last Line of $RCSfile$ -->