<document>
  <header>
    <product>resin</product>
    <title>Bean-style servlet</title>
    <type>tutorial</type>
    <description>Resin allows servlets to be configured with bean-style setters.</description>
    <tutorial-startpage>hello</tutorial-startpage>
  </header>

  <body>
    <summary/>

<p>With Resin, servlets can use Java Bean-style configuration.  A "Java Bean"
is just a Java class that follows a simple set of rules.  Each configuration
parameter <var>foo</var> has a corresponding setter method
<code>setFoo</code> with a single argument for the value.  Resin can
look at the class using Java's reflection and find the <code>setFoo</code>
method.  Because Resin can find the bean-style setters from looking at the
class, it can configure those setters in a configuration file
like the web.xml.</p>

<section title="Files in this tutorial">
<deftable>
<tr><td><viewfile-link file="WEB-INF/web.xml"/>
    <td>Configures the Servlet with bean-style init
<tr><td><viewfile-link file="WEB-INF/classes/test/HelloServlet.java"/>
    <td>The servlet implementation.
</deftable>
</section>

<section title="HelloServlet">

<p>The following <code>HelloServlet</code> servlet is a trivial bean-style
servlet.  Instead of hardcoding the "Hello, world" string, it lets the
web.xml configure the string as <var>greeting</var>.  To make that work,
<code>HelloWorld</code> adds a bean-style <code>setGreeting(String)</code>
jmethod.

<example title='WEB-INF/classes/test/HelloServlet.java'>
package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  private String _greeting = "Default";

  public void setGreeting(String greeting)
  {
    _greeting = greeting;
  }

  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println(_greeting);
    out.close();
  }
}
</example>

</section>

<section title="Configuration">

<p>The <a config-tag="servlet"/> configuration sets the <var>greeting</var> property
inside an <a config-tag="init/servlet"/> tag.  After Resin instantiates the servlet object,
it looks at the configuration file for any &lt;init> section.  Resin then
calls a <code>setXXX</code> method for each <var>&lt;xxx></var> tag in
&lt;init>.  In this case, Resin will call <code>setGreeting</code></p>


<p>Resin will perform any type conversion necessary, so you can use
integers and doubles as well as strings.  After Resin calls the <code>setXXX</code> methods, it will call the <code>init(ServletConfig)</code> method.</p>

<p>When Resin initializes the servlet, it will make
the following calls:</p>

<ol>
<li><code>servlet = new test.HelloServlet();</code>
<li><code>servlet.setGreeting("Hello, World!");</code>
<li><code>servlet.init(servletConfig);</code>
</ol>

<example title="WEB-INF/web.xml">
&lt;web-app xmlns="http://caucho.com/ns/resin">
  &lt;servlet servlet-name="hello"
            servlet-class="test.HelloServlet">
    &lt;init>
      &lt;greeting>Hello, World!&lt;/greeting>
    &lt;/init>
  &lt;/servlet>

  &lt;servlet-mapping url-pattern="/hello"
            servlet-name="hello"/>
&lt;/web-app>
</example>

</section>

  </body>
</document>
