Suppose we want tomcat to be able to execute the getDateAndTime servlet that was given earlier. Although we could add the files for our own servlets to the examples directory, suppose we want to store our servlets in a separate directory. Suppose this directory is called mytomcat.
We will need to create the mytomcat directory as a subdirectory of:
%CATALINA_HOME%\webappsWe actually have to create a directory tree like this:
webapps mytomcat WEB-INF classes libYou can do this in Windows Explorer, or execute the following commands in a Command Prompt window:
0034: cd %CATALINA_HOME%\webapps 0035: mkdir mytomcat 0036: cd mytomcat 0037: mkdir WEB-INF 0038: cd WEB-INF 0039: mkdir classes lib
We now need somewhere to store the servlet's source code. Although tomcat does not require the source code of a servlet to be stored in the %CATALINA_HOME% directory tree, one possibility is to create a subdirectory of:
%CATALINA_HOME%\webapps\mytomcatcalled getDateAndTime and store the text of the getDateAndTime servlet in the file:
%CATALINA_HOME%\webapps\mytomcat\getDateAndTime\getDateAndTime.java
Then execute the following commands from a Command Prompt window:
0040: cd %CATALINA_HOME%\webapps\mytomcat\getDateAndTime 0041: set classpath=..\..\..\common\lib\servlet.jar;. 0042: javac -d ..\WEB-INF\classes getDateAndTime.java 0043: dir /od ..\WEB-INF\classesThis will compile the file getDateAndTime.java. The -d option will arrange for the .class file to be put into a directory where tomcat will find it. So this javac command creates the file:
%CATALINA_HOME%\webapps\mytomcat\WEB-INF\classes\getDateAndTime.class
By convention, a servlet is accessed using a URL like:
http://localhost:8080/mytomcat/servlet/getDateAndTimeWhen it gets a URL like this, tomcat knows from the first part of the URL that the files are in:
%CATALINA_HOME%\webapps\mytomcatHowever, we have to tell tomcat that there is a servlet called getDateAndTime. We can do this by putting the following text into a file called web.xml in the:
%CATALINA_HOME%\webapps\mytomcat\WEB-INFdirectory:
0044: <?xml version="1.0" encoding="ISO-8859-1"?> 0045: <!DOCTYPE web-app 0046: PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 0047: "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> 0048: <web-app> 0049: <display-name>mytomcat</display-name> 0050: <servlet> 0051: <servlet-name>getDateAndTime</servlet-name> 0052: <display-name>getDateAndTime Servlet</display-name> 0053: <servlet-class> 0054: getDateAndTime 0055: </servlet-class> 0056: </servlet> 0057: <servlet-mapping> 0058: <servlet-name>getDateAndTime</servlet-name> 0059: <url-pattern>/servlet/getDateAndTime</url-pattern> 0060: </servlet-mapping> 0061: </web-app>
Having done that, restart tomcat,
and then use a browser to go to:
http://localhost:8080/mytomcat/servlet/getDateAndTime
This will get tomcat to execute the bytecodes of the file:
%CATALINA_HOME%\webapps\mytomcat\WEB-INF\classes\getDateAndTime.class
If you now alter the text of the servlet getDateAndTime.java, and recompile it placing the file getDateAndTime.class in the classes directory, you will find that this new version will not be executed unless you restart tomcat.
If you wish, you can alter the configuration of tomcat so that it frequently looks at the modification time of .class files in all of the classes directories and reloads its copy of any .class files that have been updated. This can be done by altering the file:
%CATALINA_HOME%\conf\server.xmladding the line:
0062: <DefaultContext debug="0" reloadable="true"/>between the line:
0063: </Context>and the line:
0064: </Host>You will have to restart tomcat for this change to server.xml to have any effect.