Adding a Bean
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

Introduction
JSP Compilation
Reference
Tutorials
Articles
FAQ

JSP Page Creation
Request
Topics
Tag Libraries

Mailing form results
Java beans
Mailing form results
Topics
Tag Libraries

Adding a Bean

This example uses a Java bean to create a simple hit counter. There's nothing complicated about Java beans. Java beans are just Java classes that follow some simple naming conventions. Resin makes creating beans simple. Resin will automatically compile Java source in the WEB-INF/classes directory.

With the configuration you're using now, WEB-INF/classes is in:

/usr/local/resin/webapps/resin-doc/WEB-INF/classes

JSP encourages beans with the jsp:useBean tag. JSP will automatically create a new bean and store it in the application object.

The example creates a Counter bean and stores it in the application object. Each request calls getHit() to get the next value of the counter.

counter.jsp
<%@ page language='java' %>
<jsp:useBean id='counter'
             scope='application'
             class='test.Counter'/>
Visitor <jsp:getProperty name='counter'
                         property='hit'/>

The source of the bean looks like:

test/Counter.java
package test;

public class Counter {
  private int hits;

  public synchronized int getHit()
  {
    return ++hits;
  }
}

Now, create the bean in the bean directory and load counter.jsp:

/usr/local/resin/webapps/resin-doc/WEB-INF/classes/test/Counter.java

You should then make some changes to Counter.java and reload to see the auto-recompilation. Also, introduce some errors to get familiar with the error reporting.

You can also compile the bean separately and then put Counter.class in

/usr/local/resin/webapps/resin-doc/WEB-INF/classes/test/Counter.class

JSP translation

The special jsp:useBean tag translates into standard JSP. Here's the translation:

<%@ page language='java' %>
<%
test.Counter counter;
synchronized (application) {
  counter = (test.Counter) application.getAttribute("counter");
  if (counter == null) {
    counter = new test.Counter();
    application.setAttribute("counter", counter);
  }
}
%>

Visitor <%= counter.getHit() %>

Autocompile Configuration

In a real development environment, your Java source directory may be different from the web server directory. The resin.conf lets you select any directory as the Java source and Java classes.

<caucho.com>
<http-server>
  <classpath id='WEB-INF/classes'
             source='/home/ferg/ws/src'/>
</http-server>
</caucho.com>


Mailing form results
Topics
Tag Libraries
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.