Compiling, Installing, and Running a Servlet
To compile a servlet, you must first ensure that you have the JAR file containing all Servlet API classes in the CLASSPATH environment variable. The JAR file is distributed with all web containers. Tomcat 5 includes it in a file called servlet-api.jar, located in the common/lib directory. On a Windows platform, you include the JAR file in the CLASSPATH like this (assuming Tomcat is installed in C:\Jakarta\jakarta-tomcat-5):
C:/> set CLASSPATH=C:\Jakarta\jakarta-tomcat-5\common\lib\servlet-api.jar; %CLASSPATH%You can then compile the HelloYou servlet from Example 4-1 with the javac command, like this:
C:/> javac HelloYou.javaTo make the servlet available to the container, you can place the resulting class file in the WEB-INF/classes directory for the example application:
C:/> copy HelloYou.class C:\Jakarta\jakarta-tomcat-5\webapps\jsfbook\WEB-INF\classesThe container looks automatically for classes in the
WEB-INF/classes directory structure, so you can
use this directory for all application class files. The
HelloYou servlet is part of the default package,
so it goes in the WEB-INF/classes directory
itself. If you use another package, say
com.mycompany, you must put the class file in a
directory under WEB-INF/classes that mirrors the
package structure, as described earlier. Alternatively, you can
package the class files in a JAR file (see the Java SDK documents for
details) and place the JAR file in the
WEB-INF/lib directory. The internal ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access