<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="using-xquery" page="querycompilation"
               subpage="runningcompiledasservlet"/>
      <!--
           Generated at 2010-10-29T18:20:48.427+01:00--><title>Saxonica: XSLT and XQuery Processing: Running a Compiled Query as a Servlet</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title"
            content="Saxonica: XSLT and XQuery Processing: Running a Compiled Query as a Servlet"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>Running a Compiled Query as a Servlet</h1>
      <p>If a query has been compiled as a servlet, the class file should be installed in the relevant application
directory of the servlet container, and registered in the <code>web.xml</code> configuration file
in the normal way.</p>
      <p>The query can then be invoked using an HTTP GET request using the name assigned to the servlet
in the configuration file</p>
      <p>By default (see below) it is possible to specify query parameters in the URL used to invoke the query. For example if the URL
is <code>http://localhost:8080/servet/qexample?x=10&amp;y=20</code>, and if the query body declares external variables
$x and $y, then these will be initialized to the values 10 and 20.</p>
      <p>The query output will be serialized and written to the HTTP response stream. Serialization parameters
can be written within the query using the <code>saxon:output</code> option declaration.</p>
      <p>When you compile a query as a servlet, the Java class generated will extend 
<code>com.saxonica.deploy.CompiledQueryServlet</code>. Alternatively, you can nominate a superclass for the
generated code, which should itself subclass <code>CompiledQueryServlet</code>, giving you more control
over the behavior of the servlet, for example the way in which parameters are supplied. This is particularly
useful if different servlets are to share resources such as access to in-memory documents.</p>
      <p>During servlet initialization, the init() method attempts to determine whether the servlet context
 includes an attribute named <code>com.saxonica.servlet.applicationdata</code> that is an instance of the
 interface <code>SharedApplicationDataHandler</code>. If this object exists, it is called on each servlet 
 request, at which
 point it has the opportunity to configure the <code>Controller</code> for the query execution, 
 for example by supplying
 parameter values, setting a <code>URIResolver</code>, or otherwise.</p>
      <p>If no <code>SharedApplicationDataHandler</code> object is available, external variables for the query can be specified
 as query parameters in the request URL. The names of query parameters are taken as local names of no-namespace
  variables, and the values are treated as <code>xs:untypedAtomic</code> strings (which will be converted to the declared type
 of the variable if necessary, provided that the code was compiled using the <code>-untyped</code> option). 
 Any query parameters that do not correspond to declared external variables in
 the query will be ignored.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="callingcompiledfromapp.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>