<document>
  <header>
    <product>resin</product>
    <title>Dependency-injection servlet configuration</title>
    <type>tutorial</type>
    <description>
      <p>
      Resin allows servlets to be configured using dependency injection.
      </p>
    </description>
    <tutorial-startpage>hello</tutorial-startpage>
  </header>

  <body>
    <summary/>

<s1>
<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>
</s1>

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

<s1 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>

</p></s1>

<s1 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&gt; section.  Resin then
calls a <code>setXXX</code> method for each <var>&lt;xxx&gt;</var> tag in
&lt;init&gt;.  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>
<li><code>servlet.setGreeting("Hello, World!");</code></li>
<li><code>servlet.init(servletConfig);</code></li>
</ol>

<example title="WEB-INF/web.xml">
&lt;web-app xmlns="http://caucho.com/ns/resin"
            xmlns:test="urn:java:test"
            xmlns:ee="urn:java:ee"&gt;
  
  &lt;test:HelloServlet>
    &lt;ee:WebServlet urlPatterns="/hello"/>

    &lt;test:greeting>Hello, bean-style World&lt;/test:greeting>
  &lt;/test:HelloServlet>
  
&lt;/web-app&gt;
</example>

</s1>

  </body>
</document>
