• hessian
  • quercus/php
  • resin 3.0
  • resin 3.1
  • resin 4.0

  • changes
  • configuration
  • examples
  • installing
  • overview
  • starting

  • guide: admin
  • admin
  • amber
  • bam
  • caching
  • clustering
  • comet
  • database
  • deployment
  • ejb 3.0
  • embedding
  • filters
  • ioc
  • jsf
  • jsp
  • logging
  • messaging
  • quercus
  • remoting
  • security
  • resources
  • servlets
  • third-party
  • troubleshooting
  • virtual host
  • watchdog
  • webapp
  • HOME
  • ABOUT
  • ARTICLES
  • WORKSPACE
  • NEWS
  • PROJECTS
  • PRODUCTS
  • STORE
resin example $resin/java_tut/bean.xtp Using a Java bean to create a simple hit counter

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:

/var/resin/hosts/www.caucho.com/webapps/resin-3.0/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.

<%@ 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:

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: /var/resin/hosts/www.caucho.com/webapps/resin-3.0/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 /var/resin/hosts/www.caucho.com/webapps/resin-3.0/WEB-INF/classes/test/Counter.class

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() %>

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>
  • HOME |
  • CONTACT US |
  • DOCUMENTATION |
  • SALES |
  • WIKI
Copyright (c) 1998-2009 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.