Resin 1.1
FAQ
Reference
JavaDoc
Demo
Java Tutorial
Getting Started
Configuration
Topics
JSP
XTP/XSL
JavaScript
JS Library
Servlet
Virtual Hosts
Caching
Balancing
Servlet
Servlets are Java classes which service HTTP requests. The only requirement for writing a servlet is that it implements the Servlet interface. Servlets are loaded from the classpath like all Java classes. Normally, users put servlets in WEB-INF/classes so Resin will automatically reload them when they change. JSP pages are actually implemented as Servlet, but tend to be more efficient for pages with lots of text.
Basic Example
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 http://localhost/caucho-status will show /hello as a dispatching URL. The servlet-mapping tells Resin and Apache that the URL /hello should invoke the hello-world servlet. The servlet tells Resin that hello-world uses the test.HelloWorld class and that the value of the greeting init parameter is Hello World.
<caucho.com> <http-server app-dir='/usr/local/resin/doc'> <servlet-mapping url-pattern='/hello' servlet-name='hello-world'/> <servlet servlet-name='hello-world' servlet-class='test.HelloWorld'> <init-param greeting='Hello, World'/> </servlet> </http-server> </caucho.com>
As indicated by the app-dir, HelloWorld.java belongs in
/usr/local/resin/doc/WEB-INF/classes/test/HelloWorld.java
Or, if you're compiling the servlet yourself, the class file belongs in
/usr/local/resin/doc/WEB-INF/classes/test/HelloWorld.class
Following is the actual servlet code. It just prints a trivial HTML page filled with the greeting specified in the resin.conf. init() and destroy() are included mostly for illustration. Resin will call init() when it starts the servlet and destroy before Resin destroys it.
package test; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { private String greeting; public void init() throws ServletException { greeting = getInitParameter("greeting"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getPrintWriter(); pw.println("<title>" + greeting + "</title>"); pw.println("<h1>" + greeting + "</h1>"); } public void destroy() { // nothing to do } }
Topics |  | Virtual Hosts
Copyright © 1998-2000 Caucho Technology. All rights reserved. Last modified: Tue, 21 Mar 2000 10:26:08 -0800 (PST)