| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|---|
| 2 | <html>
|
|---|
| 3 | <head>
|
|---|
| 4 | <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|---|
| 5 | <title>Servlets</title>
|
|---|
| 6 | <link type="text/css" href="skin/page.css" rel="stylesheet">
|
|---|
| 7 | </head>
|
|---|
| 8 | <body text="#000000" bgcolor="#FFFFFF">
|
|---|
| 9 | <!--================= start Navigation Path ==================-->
|
|---|
| 10 | <table summary="navigation path" width="100%" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 11 | <tr>
|
|---|
| 12 | <td nowrap="nowrap" valign="middle" bgcolor="#CFDCED" height="20"><img height="1" width="5" alt="" border="0" src="skin/images/spacer.gif"><!--===== breadcrumb trail (javascript-generated) ====--><font size="2" face="Arial, Helvetica, Sans-serif"><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script></font></td>
|
|---|
| 13 | </tr>
|
|---|
| 14 | <tr>
|
|---|
| 15 | <td bgcolor="#4C6C8F" height="2"><img height="2" width="2" alt="" border="0" src="skin/images/spacer.gif"></td>
|
|---|
| 16 | </tr>
|
|---|
| 17 | </table>
|
|---|
| 18 | <!--================= end Navigation Path ==================-->
|
|---|
| 19 | <!--================= start Banner ==================-->
|
|---|
| 20 | <table summary="header with logos" width="100%" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 21 | <tr>
|
|---|
| 22 | <!--================= start Group Logo ==================-->
|
|---|
| 23 | <td bgcolor="#294563"><a href="http://xml.apache.org/"><img border="0" class="logoImage" alt="Apache XML" src="images/group-logo.gif"></a></td>
|
|---|
| 24 | <!--================= end Group Logo ==================-->
|
|---|
| 25 | <!--================= start Project Logo ==================--><td width="100%" align="center" bgcolor="#294563"><a href="http://xml.apache.org/fop/"><img border="0" class="logoImage" alt="FOP" src="images/logo.jpg"></a></td>
|
|---|
| 26 | <!--================= end Project Logo ==================-->
|
|---|
| 27 | <!--================= start Search ==================--><td valign="top" rowspan="2" bgcolor="#294563">
|
|---|
| 28 | <form target="_blank" action="http://www.google.com/search" method="get">
|
|---|
| 29 | <table summary="search" border="0" cellspacing="0" cellpadding="0" bgcolor="#4C6C8F">
|
|---|
| 30 | <tr>
|
|---|
| 31 | <td colspan="3"><img height="10" width="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 32 | </tr>
|
|---|
| 33 | <tr>
|
|---|
| 34 | <td><img height="1" width="1" alt="" src="skin/images/spacer.gif"></td><td nowrap="nowrap"><input value="xml.apache.org" name="sitesearch" type="hidden"><input size="15" name="q" id="query" type="text"><img height="1" width="5" alt="" src="skin/images/spacer.gif"><input name="Search" value="Search" type="submit">
|
|---|
| 35 | <br>
|
|---|
| 36 | <font face="Arial, Helvetica, Sans-serif" size="2" color="white">
|
|---|
| 37 | the Apache XML site
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | </font></td><td><img height="1" width="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 41 | </tr>
|
|---|
| 42 | <tr>
|
|---|
| 43 | <td><img alt="" border="0" height="10" width="9" src="skin/images/search-left.gif"></td><td><img height="1" width="1" alt="" src="skin/images/spacer.gif"></td><td><img alt="" border="0" height="10" width="9" src="skin/images/search-right.gif"></td>
|
|---|
| 44 | </tr>
|
|---|
| 45 | </table>
|
|---|
| 46 | </form>
|
|---|
| 47 | </td>
|
|---|
| 48 | <!--================= start Search ==================--><td bgcolor="#294563"><img height="10" width="10" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 49 | </tr>
|
|---|
| 50 | <tr>
|
|---|
| 51 | <td valign="bottom" bgcolor="#294563" colspan="2">
|
|---|
| 52 | <!--================= start Tabs ==================-->
|
|---|
| 53 | <div class="tab">
|
|---|
| 54 | <table summary="tab bar" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 55 | <tr>
|
|---|
| 56 | <td width="8"><img alt="" height="8" width="8" src="skin/images/spacer.gif"></td><td valign="bottom">
|
|---|
| 57 | <table summary="selected tab" style="height: 1.7em" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 58 | <tr>
|
|---|
| 59 | <td valign="top" width="5" bgcolor="#4C6C8F"><img height="5" width="5" alt="" src="skin/images/tabSel-left.gif"></td><td valign="middle" bgcolor="#4C6C8F"><font color="#ffffff" size="2" face="Arial, Helvetica, Sans-serif"><b><a href="index.html"><font color="#000000">Home</font></a></b></font></td><td valign="top" width="5" bgcolor="#4C6C8F"><img height="5" width="5" alt="" src="skin/images/tabSel-right.gif"></td>
|
|---|
| 60 | </tr>
|
|---|
| 61 | </table>
|
|---|
| 62 | </td><td width="8"><img alt="" height="8" width="8" src="skin/images/spacer.gif"></td><td valign="bottom">
|
|---|
| 63 | <table summary="non selected tab" style="height: 1.6em" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 64 | <tr>
|
|---|
| 65 | <td valign="top" width="5" bgcolor="#B2C4E0"><img height="5" width="5" alt="" src="skin/images/tab-left.gif"></td><td valign="middle" bgcolor="#B2C4E0"><a href="dev/index.html"><font face="Arial, Helvetica, Sans-serif">Development</font></a></td><td valign="top" width="5" bgcolor="#B2C4E0"><img height="5" width="5" alt="" src="skin/images/tab-right.gif"></td>
|
|---|
| 66 | </tr>
|
|---|
| 67 | </table>
|
|---|
| 68 | </td>
|
|---|
| 69 | </tr>
|
|---|
| 70 | </table>
|
|---|
| 71 | </div>
|
|---|
| 72 | <!--================= end Tabs ==================-->
|
|---|
| 73 | </td><td bgcolor="#294563"><img alt="" width="1" height="1" src="skin/images/spacer.gif"></td>
|
|---|
| 74 | </tr>
|
|---|
| 75 | <tr>
|
|---|
| 76 | <td bgcolor="#4C6C8F" colspan="4"><img width="1" height="10" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 77 | </tr>
|
|---|
| 78 | </table>
|
|---|
| 79 | <!--================= end Banner ==================-->
|
|---|
| 80 | <!--================= start Menu, NavBar, Content ==================-->
|
|---|
| 81 | <table summary="page content" bgcolor="#ffffff" width="100%" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 82 | <tr>
|
|---|
| 83 | <td valign="top">
|
|---|
| 84 | <table summary="menu" border="0" cellspacing="0" cellpadding="0">
|
|---|
| 85 | <tr>
|
|---|
| 86 | <!--================= start left top NavBar ==================-->
|
|---|
| 87 | <td rowspan="3" valign="top">
|
|---|
| 88 | <table summary="blue line" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 89 | <tr>
|
|---|
| 90 | <td bgcolor="#294563"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 91 | </tr>
|
|---|
| 92 | <tr>
|
|---|
| 93 | <td bgcolor="#CFDCED"><font color="#4C6C8F" size="4" face="Arial, Helvetica, Sans-serif"> </font></td>
|
|---|
| 94 | </tr>
|
|---|
| 95 | <tr>
|
|---|
| 96 | <td bgcolor="#294563"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 97 | </tr>
|
|---|
| 98 | </table>
|
|---|
| 99 | </td>
|
|---|
| 100 | <!--================= end left top NavBar ==================--><td bgcolor="#294563"><img width="1" height="1" alt="" src="skin/images/spacer.gif"></td><td valign="bottom" bgcolor="#4C6C8F"><img width="10" height="10" alt="" src="skin/images/spacer.gif"></td><td nowrap="nowrap" valign="top" bgcolor="#4C6C8F">
|
|---|
| 101 | <!--================= start Menu items ==================-->
|
|---|
| 102 | <div class="menu">
|
|---|
| 103 | <ul>
|
|---|
| 104 | <li>
|
|---|
| 105 | <font color="#CFDCED">Home</font>
|
|---|
| 106 | <ul>
|
|---|
| 107 |
|
|---|
| 108 | <li>
|
|---|
| 109 | <a href="index.html">Introduction</a>
|
|---|
| 110 | </li>
|
|---|
| 111 |
|
|---|
| 112 | </ul>
|
|---|
| 113 | </li>
|
|---|
| 114 | <li>
|
|---|
| 115 | <font color="#CFDCED">Using FOP</font>
|
|---|
| 116 | <ul>
|
|---|
| 117 |
|
|---|
| 118 | <li>
|
|---|
| 119 | <a href="relnotes.html">Release Notes</a>
|
|---|
| 120 | </li>
|
|---|
| 121 |
|
|---|
| 122 | <li>
|
|---|
| 123 | <a href="download.html">Download</a>
|
|---|
| 124 | </li>
|
|---|
| 125 |
|
|---|
| 126 | <li>
|
|---|
| 127 | <a href="compiling.html">Build</a>
|
|---|
| 128 | </li>
|
|---|
| 129 |
|
|---|
| 130 | <li>
|
|---|
| 131 | <a href="configuration.html">Configure</a>
|
|---|
| 132 | </li>
|
|---|
| 133 |
|
|---|
| 134 | <li>
|
|---|
| 135 | <a href="running.html">Run</a>
|
|---|
| 136 | </li>
|
|---|
| 137 |
|
|---|
| 138 | <li>
|
|---|
| 139 | <a href="embedding.html">Embed</a>
|
|---|
| 140 | </li>
|
|---|
| 141 |
|
|---|
| 142 | <li>
|
|---|
| 143 | <span class="sel"><font color="#ffcc00">Servlets</font></span>
|
|---|
| 144 | </li>
|
|---|
| 145 |
|
|---|
| 146 | <li>
|
|---|
| 147 | <a href="anttask.html">Ant task</a>
|
|---|
| 148 | </li>
|
|---|
| 149 |
|
|---|
| 150 | </ul>
|
|---|
| 151 | </li>
|
|---|
| 152 | <li>
|
|---|
| 153 | <font color="#CFDCED">Features</font>
|
|---|
| 154 | <ul>
|
|---|
| 155 |
|
|---|
| 156 | <li>
|
|---|
| 157 | <a href="compliance.html">Compliance</a>
|
|---|
| 158 | </li>
|
|---|
| 159 |
|
|---|
| 160 | <li>
|
|---|
| 161 | <a href="output.html">Output Targets</a>
|
|---|
| 162 | </li>
|
|---|
| 163 |
|
|---|
| 164 | <li>
|
|---|
| 165 | <a href="pdfencryption.html">PDF encryption</a>
|
|---|
| 166 | </li>
|
|---|
| 167 |
|
|---|
| 168 | <li>
|
|---|
| 169 | <a href="graphics.html">Graphics</a>
|
|---|
| 170 | </li>
|
|---|
| 171 |
|
|---|
| 172 | <li>
|
|---|
| 173 | <a href="fonts.html">Fonts</a>
|
|---|
| 174 | </li>
|
|---|
| 175 |
|
|---|
| 176 | <li>
|
|---|
| 177 | <a href="hyphenation.html">Hyphenation</a>
|
|---|
| 178 | </li>
|
|---|
| 179 |
|
|---|
| 180 | <li>
|
|---|
| 181 | <a href="extensions.html">Extensions</a>
|
|---|
| 182 | </li>
|
|---|
| 183 |
|
|---|
| 184 | </ul>
|
|---|
| 185 | </li>
|
|---|
| 186 | <li>
|
|---|
| 187 | <font color="#CFDCED">Resources</font>
|
|---|
| 188 | <ul>
|
|---|
| 189 |
|
|---|
| 190 | <li>
|
|---|
| 191 | <a href="gethelp.html">Getting Help</a>
|
|---|
| 192 | </li>
|
|---|
| 193 |
|
|---|
| 194 | <li>
|
|---|
| 195 | <a href="faq.html">FAQs</a>
|
|---|
| 196 | </li>
|
|---|
| 197 |
|
|---|
| 198 | <li>
|
|---|
| 199 | <a href="fo.html">XSL-FO</a>
|
|---|
| 200 | </li>
|
|---|
| 201 |
|
|---|
| 202 | <li>
|
|---|
| 203 | <a href="examples.html">Examples</a>
|
|---|
| 204 | </li>
|
|---|
| 205 |
|
|---|
| 206 | <li>
|
|---|
| 207 | <a href="maillist.html">Mailing Lists</a>
|
|---|
| 208 | </li>
|
|---|
| 209 |
|
|---|
| 210 | <li>
|
|---|
| 211 | <a href="bugs.html">Bugs</a>
|
|---|
| 212 | </li>
|
|---|
| 213 |
|
|---|
| 214 | <li>
|
|---|
| 215 | <a href="license.html">License</a>
|
|---|
| 216 | </li>
|
|---|
| 217 |
|
|---|
| 218 | <li>
|
|---|
| 219 | <a href="resources.html">Other</a>
|
|---|
| 220 | </li>
|
|---|
| 221 |
|
|---|
| 222 | </ul>
|
|---|
| 223 | </li>
|
|---|
| 224 | <li>
|
|---|
| 225 | <font color="#CFDCED">Project</font>
|
|---|
| 226 | <ul>
|
|---|
| 227 |
|
|---|
| 228 | <li>
|
|---|
| 229 | <a href="news.html">News</a>
|
|---|
| 230 | </li>
|
|---|
| 231 |
|
|---|
| 232 | <li>
|
|---|
| 233 | <a href="logocontest.html">Logo contest</a>
|
|---|
| 234 | </li>
|
|---|
| 235 |
|
|---|
| 236 | <li>
|
|---|
| 237 | <a href="status.html">Status</a>
|
|---|
| 238 | </li>
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | <li>
|
|---|
| 242 | <a href="team.html">Team</a>
|
|---|
| 243 | </li>
|
|---|
| 244 |
|
|---|
| 245 | </ul>
|
|---|
| 246 | </li>
|
|---|
| 247 | </ul>
|
|---|
| 248 | </div>
|
|---|
| 249 | <!--================= end Menu items ==================-->
|
|---|
| 250 | </td><td valign="bottom" bgcolor="#4C6C8F"><img width="10" height="10" alt="" src="skin/images/spacer.gif"></td><td bgcolor="#294563"><img width="1" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 251 | </tr>
|
|---|
| 252 | <tr>
|
|---|
| 253 | <td valign="bottom" align="left" colspan="2" rowspan="2" bgcolor="#4C6C8F"><img height="10" width="10" border="0" alt="" src="skin/images/menu-left.gif"></td><td bgcolor="#4C6C8F"><img height="10" width="10" border="0" alt="" src="skin/images/spacer.gif"></td><td valign="bottom" align="right" colspan="2" rowspan="2" bgcolor="#4C6C8F"><img height="10" width="10" border="0" alt="" src="skin/images/menu-right.gif"></td>
|
|---|
| 254 | </tr>
|
|---|
| 255 | <tr>
|
|---|
| 256 | <td height="1" bgcolor="#294563"><img width="1" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 257 | </tr>
|
|---|
| 258 | </table>
|
|---|
| 259 | </td><td valign="top" width="100%">
|
|---|
| 260 | <table summary="content" width="100%" border="0" cellpadding="0" cellspacing="0">
|
|---|
| 261 | <!--================= start middle NavBar ==================-->
|
|---|
| 262 | <tr>
|
|---|
| 263 | <td colspan="4" bgcolor="#294563"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 264 | </tr>
|
|---|
| 265 | <tr>
|
|---|
| 266 | <td align="left" width="10" bgcolor="#CFDCED"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td><td align="left" width="50%" bgcolor="#CFDCED"><font color="#4C6C8F" size="3" face="Arial, Helvetica, Sans-serif">
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 | </font><img width="10" height="8" alt="" src="skin/images/spacer.gif"></td><td align="right" width="50%" bgcolor="#CFDCED"><font color="#4C6C8F" size="3" face="Arial, Helvetica, Sans-serif">
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 | </font><img width="10" height="8" alt="" src="skin/images/spacer.gif"></td><td width="10" bgcolor="#CFDCED"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 273 | </tr>
|
|---|
| 274 | <tr>
|
|---|
| 275 | <td colspan="4" bgcolor="#294563"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 276 | </tr>
|
|---|
| 277 | <!--================= end middle NavBar ==================-->
|
|---|
| 278 | <!--================= start Content==================-->
|
|---|
| 279 | <tr>
|
|---|
| 280 | <td align="left" width="10"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td><td align="left" width="100%">
|
|---|
| 281 | <div class="content">
|
|---|
| 282 | <table class="title" summary="">
|
|---|
| 283 | <tr>
|
|---|
| 284 | <td valign="middle">
|
|---|
| 285 | <h1>Servlets</h1>
|
|---|
| 286 | </td><td nowrap="nowrap" width="80" align="center"><a class="dida" href="servlets.pdf"><img alt="printer" src="skin/images/printer.gif" border="0"><br>
|
|---|
| 287 | print-friendly<br>
|
|---|
| 288 | PDF</a></td>
|
|---|
| 289 | </tr>
|
|---|
| 290 | </table>
|
|---|
| 291 | <h3>How to use FOP in a Servlet</h3>
|
|---|
| 292 | <ul class="minitoc">
|
|---|
| 293 | <li>
|
|---|
| 294 | <a href="#overview">Overview</a>
|
|---|
| 295 | </li>
|
|---|
| 296 | <li>
|
|---|
| 297 | <a href="#example-servlets">Example Servlets in the FOP distribution</a>
|
|---|
| 298 | </li>
|
|---|
| 299 | <li>
|
|---|
| 300 | <a href="#servlet">Create your own Servlet</a>
|
|---|
| 301 | <ul class="minitoc">
|
|---|
| 302 | <li>
|
|---|
| 303 | <a href="#minimal-servlet">A minimal Servlet</a>
|
|---|
| 304 | </li>
|
|---|
| 305 | <li>
|
|---|
| 306 | <a href="#xslt">Adding XSL tranformation (XSLT)</a>
|
|---|
| 307 | </li>
|
|---|
| 308 | <li>
|
|---|
| 309 | <a href="#cfg">Custom configuration</a>
|
|---|
| 310 | </li>
|
|---|
| 311 | <li>
|
|---|
| 312 | <a href="#performance">Improving performance</a>
|
|---|
| 313 | </li>
|
|---|
| 314 | </ul>
|
|---|
| 315 | </li>
|
|---|
| 316 | <li>
|
|---|
| 317 | <a href="#ie">Notes on Microsoft Internet Explorer</a>
|
|---|
| 318 | </li>
|
|---|
| 319 | <li>
|
|---|
| 320 | <a href="#servlet-engine">Servlet Engines</a>
|
|---|
| 321 | <ul class="minitoc">
|
|---|
| 322 | <li>
|
|---|
| 323 | <a href="#tomcat">Tomcat</a>
|
|---|
| 324 | </li>
|
|---|
| 325 | <li>
|
|---|
| 326 | <a href="#websphere">WebSphere 3.5</a>
|
|---|
| 327 | </li>
|
|---|
| 328 | </ul>
|
|---|
| 329 | </li>
|
|---|
| 330 | <li>
|
|---|
| 331 | <a href="#complex-usecases">Handling complex use cases</a>
|
|---|
| 332 | </li>
|
|---|
| 333 | </ul>
|
|---|
| 334 |
|
|---|
| 335 | <a name="N101C1"></a><a name="overview"></a>
|
|---|
| 336 | <h3>Overview</h3>
|
|---|
| 337 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 338 | <p>
|
|---|
| 339 | This page discusses topic all around using FOP in a servlet environment.
|
|---|
| 340 | </p>
|
|---|
| 341 | </div>
|
|---|
| 342 |
|
|---|
| 343 | <a name="N101CB"></a><a name="example-servlets"></a>
|
|---|
| 344 | <h3>Example Servlets in the FOP distribution</h3>
|
|---|
| 345 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 346 | <p>
|
|---|
| 347 | In the directory {fop-dir}/examples/servlet, you'll find a working example
|
|---|
| 348 | of a FOP-enabled servlet.
|
|---|
| 349 | </p>
|
|---|
| 350 | <p>
|
|---|
| 351 | You can build the servlet easily by using the supplied Ant script. After building
|
|---|
| 352 | the servlet, drop fop.war into the webapps directory of Tomcat. Then, you can use
|
|---|
| 353 | URLs like the following to generate PDF files:
|
|---|
| 354 | </p>
|
|---|
| 355 | <ul>
|
|---|
| 356 |
|
|---|
| 357 | <li>http://localhost:8080/fop/fop?fo=/home/path/to/fofile.fo</li>
|
|---|
| 358 |
|
|---|
| 359 | <li>http://localhost:8080/fop/fop?xml=/home/path/to/xmlfile.xml&xsl=/home/path/to/xslfile.xsl</li>
|
|---|
| 360 |
|
|---|
| 361 | </ul>
|
|---|
| 362 | <p></p>
|
|---|
| 363 | <p>The source code for the servlet can be found under {fop-dir}/examples/servlet/src/FopServlet.java.</p>
|
|---|
| 364 | </div>
|
|---|
| 365 |
|
|---|
| 366 | <a name="N101E6"></a><a name="servlet"></a>
|
|---|
| 367 | <h3>Create your own Servlet</h3>
|
|---|
| 368 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 369 | <div class="frame note">
|
|---|
| 370 | <div class="label">Note</div>
|
|---|
| 371 | <div class="content">
|
|---|
| 372 | This section assumes you are familiar with <a href="embedding.html">embedding FOP</a>.
|
|---|
| 373 | </div>
|
|---|
| 374 | </div>
|
|---|
| 375 | <a name="N101F3"></a><a name="minimal-servlet"></a>
|
|---|
| 376 | <h4>A minimal Servlet</h4>
|
|---|
| 377 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 378 | <p>
|
|---|
| 379 | Here is a minimal code snippet to demonstrate the basics:
|
|---|
| 380 | </p>
|
|---|
| 381 | <pre class="code">public void doGet(HttpServletRequest request,
|
|---|
| 382 | HttpServletResponse response) throws ServletException {
|
|---|
| 383 | try {
|
|---|
| 384 | response.setContentType("application/pdf");
|
|---|
| 385 | Driver driver = new Driver(new InputSource("foo.fo"),
|
|---|
| 386 | response.getOutputStream());
|
|---|
| 387 | driver.setRenderer(Driver.RENDER_PDF);
|
|---|
| 388 | driver.run();
|
|---|
| 389 | } catch (Exception ex) {
|
|---|
| 390 | throw new ServletException(ex);
|
|---|
| 391 | }
|
|---|
| 392 | }</pre>
|
|---|
| 393 | <div class="frame note">
|
|---|
| 394 | <div class="label">Note</div>
|
|---|
| 395 | <div class="content">
|
|---|
| 396 | There are numerous problems with the code snippet above.
|
|---|
| 397 | Its purpose is only to demonstrate the basic concepts.
|
|---|
| 398 | See below for details.
|
|---|
| 399 | </div>
|
|---|
| 400 | </div>
|
|---|
| 401 | </div>
|
|---|
| 402 | <a name="N10204"></a><a name="xslt"></a>
|
|---|
| 403 | <h4>Adding XSL tranformation (XSLT)</h4>
|
|---|
| 404 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 405 | <p>
|
|---|
| 406 | A common requirement is the to transform an XML source to
|
|---|
| 407 | XSLFO using an XSL transformation. It is recommended to use
|
|---|
| 408 | JAXP for this task. The following snippet shows the basic
|
|---|
| 409 | code:
|
|---|
| 410 | </p>
|
|---|
| 411 | <pre class="code">
|
|---|
| 412 | protected Logger log;
|
|---|
| 413 | protected TransformerFactory transformerFactory;
|
|---|
| 414 |
|
|---|
| 415 | public void init() throws ServletException {
|
|---|
| 416 | this.log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
|
|---|
| 417 | this.transformerFactory = TransformerFactory.newInstance();
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | [..]
|
|---|
| 421 |
|
|---|
| 422 | //Setup FOP
|
|---|
| 423 | Driver driver = new Driver();
|
|---|
| 424 | driver.setLogger(this.log);
|
|---|
| 425 | driver.setRenderer(Driver.RENDER_PDF);
|
|---|
| 426 |
|
|---|
| 427 | //Setup a buffer to obtain the content length
|
|---|
| 428 | ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|---|
| 429 | driver.setOutputStream(out);
|
|---|
| 430 |
|
|---|
| 431 | //Setup Transformer
|
|---|
| 432 | Source xsltSrc = new StreamSource(new File("foo-xml2fo.xsl"));
|
|---|
| 433 | Transformer transformer = this.transformerFactory.newTransformer(xsltSrc);
|
|---|
| 434 |
|
|---|
| 435 | //Make sure the XSL transformation's result is piped through to FOP
|
|---|
| 436 | Result res = new SAXResult(driver.getContentHandler());
|
|---|
| 437 |
|
|---|
| 438 | //Setup input
|
|---|
| 439 | Source src = new StreamSource(new File("foo.xml"));
|
|---|
| 440 |
|
|---|
| 441 | //Start the transformation and rendering process
|
|---|
| 442 | transformer.transform(src, res);
|
|---|
| 443 |
|
|---|
| 444 | //Prepare response
|
|---|
| 445 | response.setContentType("application/pdf");
|
|---|
| 446 | response.setContentLength(out.size());
|
|---|
| 447 |
|
|---|
| 448 | //Send content to Browser
|
|---|
| 449 | response.getOutputStream().write(out.toByteArray());
|
|---|
| 450 | response.getOutputStream().flush();</pre>
|
|---|
| 451 | <div class="frame note">
|
|---|
| 452 | <div class="label">Note</div>
|
|---|
| 453 | <div class="content">
|
|---|
| 454 | Buffering the generated PDF in a ByteArrayOutputStream is done to avoid potential
|
|---|
| 455 | problems with the Acrobat Reader Plug-in in IEx.
|
|---|
| 456 | </div>
|
|---|
| 457 | </div>
|
|---|
| 458 | <p>
|
|---|
| 459 | The <span class="codefrag">Source</span> instance used above is simply an
|
|---|
| 460 | example. If you have to read the XML from a string, supply
|
|---|
| 461 | a <span class="codefrag">new StreamSource(new
|
|---|
| 462 | StringReader(xmlstring))</span>. Constructing and reparsing
|
|---|
| 463 | an XML string is generally less desirable than using a
|
|---|
| 464 | SAXSource if you generate your XML. You can alternatively
|
|---|
| 465 | supply a DOMSource as well. You may also use dynamically
|
|---|
| 466 | generated XSL if you like.
|
|---|
| 467 | </p>
|
|---|
| 468 | <p>
|
|---|
| 469 | Because you have an explicit <span class="codefrag">Transformer</span> object, you can also use it to
|
|---|
| 470 | explicitly set parameters for the transformation run.
|
|---|
| 471 | </p>
|
|---|
| 472 | </div>
|
|---|
| 473 | <a name="N10224"></a><a name="cfg"></a>
|
|---|
| 474 | <h4>Custom configuration</h4>
|
|---|
| 475 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 476 | <p>
|
|---|
| 477 | If you need to supply a special configuration do this in the <span class="codefrag">init()</span>
|
|---|
| 478 | method so it will only be done once and to avoid multithreading problems.
|
|---|
| 479 | </p>
|
|---|
| 480 | <pre class="code">public void init() throws ServletException {
|
|---|
| 481 | [..]
|
|---|
| 482 | new Options(new File("userconfig.xml"));
|
|---|
| 483 | //or
|
|---|
| 484 | Configuration.put("baseDir", "/my/base/dir");
|
|---|
| 485 | }</pre>
|
|---|
| 486 | </div>
|
|---|
| 487 | <a name="N10235"></a><a name="performance"></a>
|
|---|
| 488 | <h4>Improving performance</h4>
|
|---|
| 489 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 490 | <p>
|
|---|
| 491 | There are several options to consider:
|
|---|
| 492 | </p>
|
|---|
| 493 | <ul>
|
|---|
| 494 |
|
|---|
| 495 | <li>
|
|---|
| 496 | Instead of java.io.ByteArrayOutputStream consider using the ByteArrayOutputStream
|
|---|
| 497 | implementation from the Jakarta Commons IO project which allocates less memory.
|
|---|
| 498 | </li>
|
|---|
| 499 |
|
|---|
| 500 | <li>
|
|---|
| 501 | In certain cases it can help to write the generated PDF to a temporary file so
|
|---|
| 502 | you can quickly reuse the file. This is especially useful, if Internet Explorer
|
|---|
| 503 | calls the servlet multiple times with the same request or if you often generate
|
|---|
| 504 | equal PDFs.
|
|---|
| 505 | </li>
|
|---|
| 506 |
|
|---|
| 507 | </ul>
|
|---|
| 508 | <p>
|
|---|
| 509 | Of course, the
|
|---|
| 510 | <a href="embedding.html#performance">performance hints from the Embedding page</a>
|
|---|
| 511 | apply here, too.
|
|---|
| 512 | </p>
|
|---|
| 513 | </div>
|
|---|
| 514 | </div>
|
|---|
| 515 |
|
|---|
| 516 | <a name="N10250"></a><a name="ie"></a>
|
|---|
| 517 | <h3>Notes on Microsoft Internet Explorer</h3>
|
|---|
| 518 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 519 | <p>
|
|---|
| 520 | Some versions of Internet Explorer will not automatically show the PDF or call the servlet multiple times.
|
|---|
| 521 | These are well-known limitations of Internet Explorer and are not a problem of the servlet.
|
|---|
| 522 | However, Internet Explorer can still be used to download the PDF so that it can be viewed later.
|
|---|
| 523 | Here are some suggestions in this context:
|
|---|
| 524 | </p>
|
|---|
| 525 | <ul>
|
|---|
| 526 |
|
|---|
| 527 | <li>
|
|---|
| 528 | Use an URL ending in <span class="codefrag">.pdf</span>, like
|
|---|
| 529 | <span class="codefrag">http://myserver/servlet/stuff.pdf</span>. Yes, the servlet can
|
|---|
| 530 | be configured to handle this. If the URL has to contain parameters,
|
|---|
| 531 | try to have <strong>both</strong> the base URL as well as the last parameter end in
|
|---|
| 532 | <span class="codefrag">.pdf</span>, if necessary append a dummy parameter, like
|
|---|
| 533 | <span class="codefrag">http://myserver/servlet/stuff.pdf?par1=a&par2=b&d=.pdf</span>. The
|
|---|
| 534 | effect may depend on IEx version.
|
|---|
| 535 | </li>
|
|---|
| 536 |
|
|---|
| 537 | <li>
|
|---|
| 538 | Give IEx the opportunity to cache. In particular, ensure the
|
|---|
| 539 | server does not set any headers causing IEx not to cache the
|
|---|
| 540 | content. This may be a real problem if the document is sent
|
|---|
| 541 | over HTTPS, because most IEx installations will by default
|
|---|
| 542 | <em>not</em> cache any content retrieved over HTTPS.
|
|---|
| 543 | Setting the <span class="codefrag">Expires</span> header entry may help in
|
|---|
| 544 | this case:<br>
|
|---|
| 545 | <span class="codefrag">response.setDateHeader("Expires",
|
|---|
| 546 | System.currentTimeMillis() + cacheExpiringDuration *
|
|---|
| 547 | 1000);</span>
|
|---|
| 548 | <br> Consult your server manual and the
|
|---|
| 549 | relevant RFCs for further details on HTTP headers and
|
|---|
| 550 | caching.
|
|---|
| 551 | </li>
|
|---|
| 552 |
|
|---|
| 553 | <li>
|
|---|
| 554 | Cache in the server. It may help to include a parameter in
|
|---|
| 555 | the URL which has a timestamp as the value min order to
|
|---|
| 556 | decide whether a request is repeated. IEx is reported to
|
|---|
| 557 | retrieve a document up to three times, but never more often.
|
|---|
| 558 | </li>
|
|---|
| 559 |
|
|---|
| 560 | </ul>
|
|---|
| 561 | </div>
|
|---|
| 562 |
|
|---|
| 563 | <a name="N10281"></a><a name="servlet-engine"></a>
|
|---|
| 564 | <h3>Servlet Engines</h3>
|
|---|
| 565 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 566 | <p>
|
|---|
| 567 | When using a servlet engine, there are potential CLASSPATH issues, and potential conflicts
|
|---|
| 568 | with existing XML/XSLT libraries. Servlet containers also often use their own classloaders
|
|---|
| 569 | for loading webapps, which can cause bugs and security problems.
|
|---|
| 570 | </p>
|
|---|
| 571 | <a name="N1028A"></a><a name="tomcat"></a>
|
|---|
| 572 | <h4>Tomcat</h4>
|
|---|
| 573 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 574 | <p>
|
|---|
| 575 | Check Tomcat's documentation for detailed instructions about installing FOP and Cocoon.
|
|---|
| 576 | There are known bugs that must be addressed, particularly for Tomcat 4.0.3.
|
|---|
| 577 | </p>
|
|---|
| 578 | </div>
|
|---|
| 579 | <a name="N10294"></a><a name="websphere"></a>
|
|---|
| 580 | <h4>WebSphere 3.5</h4>
|
|---|
| 581 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 582 | <p>
|
|---|
| 583 | Put a copy of a working parser in some directory where WebSphere can access it.
|
|---|
| 584 | For example, if /usr/webapps/yourapp/servlets is the CLASSPATH for your servlets,
|
|---|
| 585 | copy the Xerces jar into it (any other directory would also be fine).
|
|---|
| 586 | Do not add the jar to the servlet CLASSPATH, but add it to the CLASSPATH of the
|
|---|
| 587 | application server which contains your web application.
|
|---|
| 588 | In the WebSphere administration console, click on the "environment" button in the
|
|---|
| 589 | "general" tab. In the "variable name" box, enter "CLASSPATH".
|
|---|
| 590 | In the "value" box, enter the correct path to the parser jar file
|
|---|
| 591 | (/usr/webapps/yourapp/servlets/Xerces.jar in our example here).
|
|---|
| 592 | Press "OK", then apply the change and restart the application server.
|
|---|
| 593 | </p>
|
|---|
| 594 | </div>
|
|---|
| 595 | </div>
|
|---|
| 596 |
|
|---|
| 597 | <a name="N1029F"></a><a name="complex-usecases"></a>
|
|---|
| 598 | <h3>Handling complex use cases</h3>
|
|---|
| 599 | <div style="margin-left: 0 ; border: 2px">
|
|---|
| 600 | <p>
|
|---|
| 601 | Sometimes the requirements for a servlet get quite sophisticated: SQL data sources,
|
|---|
| 602 | multiple XSL transformations, merging of several datasources etc. In such a case
|
|---|
| 603 | consider using <a target="_blank" href="http://cocoon.apache.org">Apache Cocoon</a> instead
|
|---|
| 604 | of a custom servlet to accomplish your goal.
|
|---|
| 605 | </p>
|
|---|
| 606 | </div>
|
|---|
| 607 |
|
|---|
| 608 | </div>
|
|---|
| 609 | </td><td width="10"><img width="10" height="1" alt="" src="skin/images/spacer.gif"></td>
|
|---|
| 610 | </tr>
|
|---|
| 611 | <!--================= end Content==================-->
|
|---|
| 612 | </table>
|
|---|
| 613 | </td>
|
|---|
| 614 | </tr>
|
|---|
| 615 | </table>
|
|---|
| 616 | <!--================= end Menu, NavBar, Content ==================-->
|
|---|
| 617 | <!--================= start Footer ==================-->
|
|---|
| 618 | <table summary="footer" cellspacing="0" cellpadding="0" width="100%" border="0">
|
|---|
| 619 | <tr>
|
|---|
| 620 | <td colspan="2" height="1" bgcolor="#4C6C8F"><img height="1" width="1" alt="" src="skin/images/spacer.gif"><a href="skin/images/label.gif"></a><a href="skin/images/page.gif"></a><a href="skin/images/chapter.gif"></a><a href="skin/images/chapter_open.gif"></a><a href="skin/images/current.gif"></a><a href="/favicon.ico"></a></td>
|
|---|
| 621 | </tr>
|
|---|
| 622 | <tr>
|
|---|
| 623 | <td colspan="2" bgcolor="#CFDCED" class="copyright" align="center"><font size="2" face="Arial, Helvetica, Sans-Serif">Copyright ©
|
|---|
| 624 | 1999-2003 The Apache Software Foundation. All rights reserved.<script type="text/javascript" language="JavaScript"><!--
|
|---|
| 625 | document.write(" - "+"Last Published: " + document.lastModified);
|
|---|
| 626 | // --></script></font></td>
|
|---|
| 627 | </tr>
|
|---|
| 628 | <tr>
|
|---|
| 629 | <td colspan="2" align="left" bgcolor="#CFDCED" class="logos"></td>
|
|---|
| 630 | </tr>
|
|---|
| 631 | </table>
|
|---|
| 632 | <!--================= end Footer ==================-->
|
|---|
| 633 | </body>
|
|---|
| 634 | </html>
|
|---|