[ Team LiB ] Previous Section Next Section

Using the invoker to Run a Servlet

There are several ways to run a servlet, but the easiest is to use the invoker servlet. Although it's not a part of servlet specification, most servlet containers make the invoker servlet available, so that you don't have to do anything special for the container to recognize your servlets. As you go through the examples in the book, you may find that's it's convenient to use this feature.

By default, Tomcat does not enable the invoker servlet; it's considered a security risk. You'll need to enable it. Looking at web.xml in the conf/ directory of your Tomcat installation, you will see the following:


<!--
    <servlet>
        <servlet-name>invoker</servlet-name>
        <servlet-class>
          org.apache.catalina.servlets.InvokerServlet
        </servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
-->

All that needs to be done is to uncomment the servlet declaration so that it looks like this:


<servlet>
    <servlet-name>invoker</servlet-name>
    <servlet-class>
      org.apache.catalina.servlets.InvokerServlet
    </servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

That makes Tomcat aware of the invoker servlet's configuration. Then, you'll need to uncomment the servlet mapping for the invoker. Uncomment this mapping:


<!-- The mapping for the invoker servlet -->
<!--
<servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
 </servlet-mapping>
-->

It should look like this when you are done:


<!-- The mapping for the invoker servlet -->

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

At this point, a servlet can be invoked by using a URL that includes the path servlet/. The invoker will try to match whatever follows servlet/ to a servlet class in the container's classpath and execute it. The URL for this example is

http://localhost:8080/servlet/examples.HelloWorldServlet

Using invoker in a Production Environment Is Unsafe

graphics/watchout_icon.gif

Although the /servlet/ pattern is useful for development, you should never use it on a production server. The /servlet/ pattern can arbitrarily load Java classes regardless of whether or not they are servlets. (It must first load them before it can determine whether they implement the Servlet interface.) A malicious person could force your container to load a class and potentially harm or compromise your server. You should always remove the /servlet/ pattern from your production servers.


The method for adding to a container's classpath varies with the container you are using. For some containers, you must edit the container's startup script, whereas others enable you to add to the classpath through an administration tool. Tomcat has a nice alternative to modifying the classpath: It gives you a place to store your class files that is already in the classpath. You copy them to common\classes\underneath the main Tomcat directory. (If you want to add JAR files, copy them to common\lib\.) Because HelloWorldServlet is packaged in examples, you must create a directory under common\classes\called examples\.

We will try out our servlet by placing HelloWorldServlet.class in common\classes\examples\.

Sharing Classes Between Web Applications

graphics/bytheway_icon.gif

Both common\classes and common\lib are actually reserved as places where you can put classes and libraries that are shared by all Web applications.


After copying HelloWorldServlet, starting Tomcat, and pointing your browser to it, you should see the result shown in Figure 2.1.

If, when trying to access your servlet, the container fails to locate your servlet the first time you try to run it, try restarting the container. If it still doesn't work, your modification to the classpath may not have worked.

Redeploying a Changed Servlet

graphics/didyouknow_icon.gif

Some servlet containers don't automatically reload a servlet after it has been loaded. If you make changes to your servlet and redeploy it, you might not see the changes. If your servlet container doesn't reload servlets, you might need to restart the servlet container to pick up the changes.


In Case of Trouble

graphics/bytheway_icon.gif

If you are having trouble running HelloWorldServlet, see "Q&A" at the end of this hour.


    [ Team LiB ] Previous Section Next Section