Bean-style servlet
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Servlets
Servlet Lib
run-at
Filters
Filter Lib
Tutorials
FAQ

Hello World
Bean-Init
Sub-Bean-Init
Filter Templates
Hello World
Tutorials
Sub-Bean-Init

Find this tutorial in: /usr/local/resin/webapps/resin-doc/servlet/tutorial/bean-init
Try the Tutorial
Resin allows servlets to be configured with bean-style setters.

  1. Files in this tutorial
  2. HelloServlet
  3. Configuration

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 foo has a corresponding setter method setFoo with a single argument for the value. Resin can look at the class using Java's reflection and find the setFoo 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.

Files in this tutorial

WEB-INF/web.xml Configures the Servlet with bean-style init
WEB-INF/classes/test/HelloServlet.java The servlet implementation.

HelloServlet

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

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();
  }
}

Configuration

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

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

When Resin initializes the servlet, it will make the following calls:

  1. servlet = new test.HelloServlet();
  2. servlet.setGreeting("Hello, World!");
  3. servlet.init(servletConfig);

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

  <servlet-mapping url-pattern="/hello"
            servlet-name="hello"/>
</web-app>

Try the Tutorial


Hello World
Tutorials
Sub-Bean-Init
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.