caucho
Resin
FAQ
Reference Guide
Demo
Tutorial

JSP page
Config
URLs
Database Forms
XTP Copy
Hello Tag
Vary Filter
HardCore
Mailing Forms
Beans
Cache
XSL Filter
run-at
 Adding a Bean

Mailing Forms
Tutorial
Cache

This example will use 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:

D:\java\resin-2.1.12\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:

D:\java\resin-2.1.12\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

D:\java\resin-2.1.12\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 Forms
Tutorial
Cache
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.