<title>Servlet</title>

<p/>Servlets are Java classes which service HTTP requests.  The only
requirement for writing a servlet is that it implements the
<a href='../javadoc/javax/servlet/Servlet.html'>Servlet</a> interface.

<p/>Servlets are loaded from the classpath like all Java classes.
Normally, users put servlets in <var/WEB-INF/classes/> so Resin will
automatically reload them when they change.

<p/><a href='jsp.xtp'>JSP</a> pages are actually implemented as
Servlet, but tend to be more efficient for pages with lots of text.

<section title='Basic Example'>

<p/>The following is a complete working resin.conf to run this example.  If
you're using a web server like Apache, you'll need to restart Apache to use
the new configuration.  After restarting the web server, the
<var/http://localhost/caucho-status/> will show <var//hello/> as a
dispatching URL.

<p/>The <var/servlet-mapping/> tells Resin and Apache that the URL
<var//hello/> should invoke the <var/hello-world/> servlet.

<p/>The <var/servlet/> tells Resin that <var/hello-world/> uses the
<var/test.HelloWorld/> class and that the value of the <var/greeting/>
init parameter is <var/Hello World/>.

<example>
&lt;caucho.com>
&lt;http-server app-dir='/usr/local/resin/doc'>

&lt;http port='8080'/>

&lt;host id=''>
&lt;web-app id='/'>
  &lt;servlet-mapping url-pattern='/hello'
                   servlet-name='hello-world'/>

  &lt;servlet servlet-name='hello-world'
           servlet-class='test.HelloWorld'>
    &lt;init-param greeting='Hello, World'/>
  &lt;/servlet>
&lt;/web-app>
&lt;host>

&lt;/http-server>
&lt;/caucho.com>
</example>

<p/>As indicated by the <var/app-dir/>, <var/HelloWorld.java/> belongs in
<def>
/usr/local/resin/doc/WEB-INF/classes/test/HelloWorld.java
</def>

<p/>Or, if you're compiling the servlet yourself, the class file belongs in
<def>
/usr/local/resin/doc/WEB-INF/classes/test/HelloWorld.class
</def>

<p/>Following is the actual servlet code.  It just prints a trivial
HTML page filled with the greeting specified in the resin.conf.

<p/><var/init()/> and <var/destroy()/> are included mostly for
illustration.  Resin will call <var/init()/> when it starts the servlet
and <var/destroy/> before Resin destroys it.

<example>
package test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends <a href='../javadoc/javax/servlet/http/HttpServlet.html'>HttpServlet</a> {
  private String greeting;

  public void <a href='../javadoc/javax/servlet/GenericServlet.html#init()'>init</a>()
    throws ServletException
  {
    greeting = <a href='../javadoc/javax/servlet/GenericServlet.html#getInitParameter(java.lang.String)'>getInitParameter</a>("greeting");
  }

  public void doGet(<a
  href='../javadoc/javax/servlet/http/HttpServletRequest.html'>HttpServletRequest</a> request, <a
  href='../javadoc/javax/servlet/http/HttpServletResponse.html'>HttpServletResponse</a> response)
    throws ServletException, IOException
  {
    PrintWriter pw = response.getWriter();

    pw.println("&lt;title>" + greeting + "&lt;/title>");
    pw.println("&lt;h1>" + greeting + "&lt;/h1>");
  }
  
  public void <a href='../javadoc/javax/servlet/GenericServlet.html#destroy()'>destroy</a>()
  {
    // nothing to do
  }
}
</example>

</section>
