<document>
  <header>
    <product>resin</product>
    <title>Hessian Service</title>
    <description>
      <p>Writing a Hessian service as a plain-old Java object (POJO)
eliminates protocol dependencies and simplifies service testing.
</p>
    </description>
    <type>tutorial</type>
    <tutorial-startpage>demo.jsp</tutorial-startpage>
  </header>

  <body>
    <summary/>

    <s1>
<p>The <a href="../hessian-add/">addition example</a> built the Hessian
service as an extension of HessianService for simplicity.  Most services
will want to be independent of the Hessian protocol itself.</p>
</s1>

<s1 title="Files in this tutorial">
<deftable>
<tr>
  <th>File</th>
  <th>Description</th>
</tr>
<tr>
  <td><viewfile-link file="WEB-INF/classes/example/MathService.java"/></td>
  <td>Interface for the math service.</td>
</tr>
<tr>
  <td><viewfile-link file="WEB-INF/classes/example/MathServiceImpl.java"/></td>
  <td>The main service implementation.</td>
</tr>
<tr>
  <td><viewfile-link file="WEB-INF/resin-web.xml"/></td>
  <td>Configures the environment</td>
</tr>
<tr>
  <td><viewfile-link file="demo.jsp"/></td>
  <td>Client JSP</td>
</tr>
<tr>
  <td><viewfile-link file="demo.php"/></td>
  <td>Client PHP</td>
</tr>
</deftable>
</s1>

<s1 title="Service Implementation">

<p>The MathService implementation is just a Java class that implements
the MatchService API.</p>

<example title="Example: MathServiceImpl.java">
package example;

public class MathServiceImpl implements MathService {
  public int add(int a, int b)
  {
    return a + b;
  }
}
</example>

</s1>

<s1 title="Remote Interface">

<p>The Java interface describes the remote API.  This example has an
addition method, <var>add()</var>.</p>

<p>Resin's proxy client implementation uses the remote interface to
expose the API to the proxy stub.  Strictly speaking, though,
the Java remote interface is not required for Hessian.  A non-Java client
will not use the Java interface, except possibly as documentation.</p>

<example title="Example: MathService.java">
package example;

public interface MathService {
  public int add(int a, int b);
}
</example>

</s1>

<s1 title="Service configuration">

<example title="Example: resin-web.xml">
&lt;web-app xmlns="http://caucho.com/ns/resin">

  &lt;servlet-mapping url-pattern="/math/*"
                   servlet-class="example.MathService">
    &lt;protocol uri="hessian:"/>
  &lt;/servlet-mapping>

  &lt;remote-client name="math">
    &lt;uri>hessian:url=${webApp.url}/math/&lt;/uri>
    &lt;interface>example.MathService&lt;/interface>
  &lt;/remote-client>

&lt;/web-app>
</example>

</s1>

<s1 title="Java Client">

<p>The client is identical to the basic example.</p>

<example title="Example: demo.jsp">
&lt;%@ page import="javax.webbeans.In" %>
&lt;%@ page import="example.MathService" %>
&lt;%!
@In MathService math;
%>
&lt;pre>
3 + 2 = &lt;%= math.add(3, 2) %>
3 - 2 = &lt;%= math.sub(3, 2) %>
3 * 2 = &lt;%= math.mul(3, 2) %>
3 / 2 = &lt;%= math.div(3, 2) %>
&lt;/pre>
</example>
<results>
3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
</results>

</s1>

<s1 title="PHP Client">

<p>The client is identical to the basic example.</p>

<example title="Example: demo.php">
&lt;?php

$math = java_bean("math");
?>
&lt;pre>
3 + 2 = &lt;?= $math->add(3, 2) ?>
3 - 2 = &lt;?= $math->sub(3, 2) ?>
3 * 2 = &lt;?= $math->mul(3, 2) ?>
3 / 2 = &lt;?= $math->div(3, 2) ?>
&lt;/pre>
</example>
<results>
3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
</results>

</s1>

  </body>
</document>
