Loader Servlet Guide

The Loader Servlet allows servlets to be loaded without having to include them in the webserver binary, and without having to specify them in the servlet configuration file. Just upload your servlet class(es) and go.

NOTE: The Loader Servlet only works on TINI firmware 1.1x and later.

The main advantage of this feature is that it allows you to update, change, and create servlets without having to stop and restart the server each time. The classes can be uploaded and then immediately used. The disadvantage is that you need to use a special URL to call the servlet.

There are four sections:

  1. Steps to Activate This Feature,
  2. Examples,
  3. Discussion, and
  4. Rapid Testing

Steps to Activate the Loader Servlet

  1. Set include.loaderServlet=true in the build.properties file.
  2. Set these properties in the servlets.props file:
    Loader.mapping=/servlet/Loader/*
    Loader.class=com.qindesign.servlet.LoaderServlet
    Loader.initParams=classpath=/web/classes

    The first and third settings can be changed to suit your needs, but this is discussed in more detail below.

  3. Build, deploy, and then start the server.

[Top]


Examples

Start by creating a simple servlet:
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Simple Hello World servlet.
 *
 * @author Shawn Silverman
 */
public class HelloWorldServlet extends HttpServlet {
    /**
     * Process a GET request.
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        resp.setContentType("text/html");
        ServletOutputStream out = resp.getOutputStream();
        out.print("<html><head><title>Hello World Servlet</title></head><body>");
        out.print("<h1>Hello, World!</h1>");
        out.print("</body></html>");
    }
}

Save this code as HelloWorldServlet.java in a convenient place.

Compile

Next, compile this into HelloWorldServlet.class. The tws_core.jar file in the lib/ directory of the Tynamo™ distribution contains the javax.servlet servlet framework, so this needs to be in your classpath. A sample command-line is (note that the line may be wrapped, but should be on one line):

javac -target 1.1 -classpath <tynamo-dist>/lib/tws_core.jar HelloWorldServlet.java

The -target 1.1 option is required if you are using JDK 1.4 or later, and is not required otherwise. Additionally, the -source 1.3 option is required if you are using JDK 1.5 or later.

Upload

Use an FTP client to place the HelloWorldServlet.class file into a directory named /web/classes on your TINI. Remember that the Loader Servlet is expecting to find classes here because that's what was specified for its classpath.

Finally, use your web browser to go to this location:

http://{TINI address}/servlet/Loader/HelloWorldServlet

The page should look like this:

Hello, World!

Change

Change the page text to read "Changed!" instead of "Hello, World!", then recompile and upload the class again.

Reload the page, and you should now see:

Changed!

Class Change

Change the classname to MyServlet (for example) by changing the source code and the file name. If you wish, change the servlet output as well.

Compile MyServlet.java and upload MyServlet.class to /web/classes on your TINI.

Now, direct your web browser to:

http://{TINI address}/servlet/Loader/MyServlet

You should see the changes you just made.

[Top]


Discussion

One of the first things you may notice is that the servlet takes longer than normal to load. This is because more work has to be done by the firmware and by the server: The classfile must be loaded, checked, and converted internally into the native TINI format. Then, the server loads the class via a Class.forName call and has to verify it has the correct superclass. Next, the servlet is initialized via its init() method, and finally its GET handler is invoked.

In fact, it's amazing how quickly the servlet loads, given all these factors.

Classnames

The full classname must be provided when loading your servlet. For example, if MyServlet exists in the package com.xxx, then the web browser request would look like this:

http://{TINI address}/servlet/Loader/com.xxx.MyServlet

Additionally, the class must be located in the correct directory structure underneath the Loader Servlet's classpath. For the above example, place the com.xxx.MyServlet class into /web/classes/com/xxx/MyServlet.class.

Regardless of how you name your class, though, all request parameters and path information will be passed as usual to your servlet.

Different Classpath

It is not required that you use the /web/classes directory for your classes. You can specify anything you like, provided the location you put them and the Loader Servlet's classpath match. Simply change the classpath initialization parameter in Loader.initParams to your desired location.

A change in this setting requires a server restart.

Different Request Path

It is also possible to change the /servlet/Loader request path if you wish. Simply change the Loader.mapping setting to something else, /doLoad/*, for example. Be sure, however, to include the /* at the end of the path.

A change in this setting requires a server restart.

Referenced Classes

It is important to note that if your servlet depends on other classes, then these must also be uploaded to the location of the Loader Servlet's classpath, using the correct directory structure if they are in a package.

[Top]


Rapid Testing

It should be apparent that very little work needs to be done in order to get basic servlet reloading with the Loader Servlet to work. You do not have to specify the servlet in the servlet configuration, nor do you have to know in advance what servlets you are going to test in the future.

This enables you to rapidly test your servlets every time a change is made. Significant amounts of time will be saved because you don't have to continuously stop and restart the server.

[Top]


The Loader Servlet is discussed in more detail in the Servlet Reloading document.