[ Team LiB ] |
![]() ![]() |
Using the invoker to Run a ServletThere 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
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
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
In Case of Trouble
|
[ Team LiB ] |
![]() ![]() |