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

Servlet
Parse Errors
Runtime Errors
Debugging
 A Hello, World Servlet

JSP page
JSP page
Parse Errors

Servlets are the pure Java solution to handle web requests. Many application will use servlets instead of JSP and others will use servlets in conjunction with JSP. Experienced JSP programmers use servlets in conjunction with JSP to create clearer and simpler applications. The servlets handle Java processing: form handing, calculation and database queries. JSP formats the results.

Servlets belong in doc/WEB-INF/classes. On this machine, you'll put the Java source in /usr/local/resin-2.1.8/doc/java_tut/WEB-INF/classes. WEB-INF/classes is the standard location for servlets and other Java classes. Resin automatically reloads and recompiles servlets, beans, and classes placed in WEB-INF/classes. You should make some changes and add errors to become familiar with Resin's recompilation and the error reporting.

Create the following servlet in WEB-INF/classes/test/HelloWorld.java with your favorite text editor: notepad, emacs, vi, or whatever. (On this machine, /usr/local/resin-2.1.8/doc/java_tut/WEB-INF/classes/test/HelloWorld.java)

WEB-INF/classes/test/HelloWorld.java
package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloWorld extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}

Now browse the servlet at http://jsp.kyokuto.co.jp:80/java_tut/servlet/test.HelloWorld. Resin will automatically compiles the servlet for you. Browsing servlets differs from page browsing because you're executing a servlet class, not looking at a page. The /servlet URL tells Resin to look for a servlet.

Configuration

The default resin.conf in the distribution is already configured for the example, so you can just start Resin as described in the standalone server. Not all of the standard resin.conf is necessary for this example, though. You can cut it down to the following configuration.

For development, using the /servlet invoker is convenient. The invoker servlet will let you specify the servlet class in the URL itself, like /servlet/test.HelloWorld. Its configuration in the resin.conf looks like:

<caucho.com>
<http-server>
  <http port='8080'/>

  <host id=''>
    <app-dir>doc</app-dir>

    <web-app id='/'>
      <servlet-mapping url-pattern='/servlet/*'
                       servlet-name='invoker'/>
    </web-app>
  </host>
</http-server>
</caucho.com>

Resin allows a short cut for the XML configuration in the example above; you can use XML attributes in place of elements. The Servlet 2.3 standard uses only elements. So the servlet-mapping configuration following the Servlet 2.3 standard would look like:

<servlet-mapping>
  <url-pattern>/servlet/*</url-pattern>
  <servlet-name>invoker</servlet-name>
</servlet-mapping>

The two are entirely equivalent. For larger configurations, using attributes makes the resin.conf or web.xml more readable.

tagmeaning
caucho.comTop-level tag for all Caucho configuration
http-serverConfiguration for the servlet engine (both http and srun)
httpConfigures a standalone HTTP server. You can listen multiple <http> or <srun> ports at once.
portThe TCP port to listen to. When actually deploying Resin, you'll use port 80, but port 8080 is a good development port.
hostConfigures the default virtual host. Many sites will only need the default.
app-dirThe document directory for the virtual host. You can change this to the location of the real document root.
web-appConfigures the root web-app for this virtual host. Most sites will only need the root web-app.
servlet-mappingSpecifies the URL to servlet mapping.
url-patternWhich URL should map to the servlet.
servlet-nameThe servlet to invoke for the URL. In this case, the special "invoker" which looks up the class from the URL.

This example just uses the default virtual host and root web-app. Many sites will just use the default virtual host and root web-app. For now, you can ignore them and concentrate on the other configuration.

The example configuration is a complete working configuration. You can create a configuration file in resin-2.0.x/conf/test.conf to test it out.

Starting on Windows
resin-2.0.x> bin\httpd -conf test.conf
Starting on Unix
resin-2.0.x> bin/httpd.sh -conf test.conf

If you run into any trouble trying to start Resin, look at the standalone HTTP server page.


JSP page
JSP page
Parse Errors
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.