Caucho Technology

a hello, world servlet

A trivial "hello, world" servlet


Files in this tutorial

WEB-INF/resin-web.xmlresin-web.xml configuration
WEB-INF/classes/test/HelloServlet.javaThe Hello, World servlet.

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 inWEB-INF/classes. On this machine, the source is in Java source inWEB-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 inWEB-INF/classes/test/HelloServlet.javawith your favorite text editor: notepad, emacs, vi, or whatever.

Example: WEB-INF/classes/test/
package test;


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

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

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

Now browse the servlet athttp://localhost:8080/resin-doc/tutorial/hello. 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/helloURL is configured for the hello, world servlet below.


Configuration for the servlet is in theWEB-INF/web.xmlfile.

The servlet needs to be configured and it needs to be mapped to a URL. Theservlettag configures the servlet. In our simple example, we just need to specify the class name for the servlet.

Theservlet-mappingtag specifies the URLs which will invoke the servlet. In our case, the/helloURL invokes the servlet. Because the tutorialwebappis a sub-URL like/doc/servlet/tutorial/helloworld, the actual URL to invoke the servlet is the combination of the two.

Example: WEB-INF/web.xml
<web-app xmlns="" version="2.4"


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.4 standard uses only elements. So the servlet-mapping configuration following the Servlet 2.4 standard would look like:

Example: WEB-INF/resin-web.xml
<web-app xmlns="">

   <servlet-mapping url-pattern="/hello"


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

web-appWeb application top-level tag.

Thexmlns=""lets Resin validate the web.xml configuration. The validator will catch most errors in the web.xml.


Copyright © 1998-2011 Caucho Technology, Inc. All rights reserved.
Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology.