If you want to install Tomcat on your home computer, follow theses steps
To test that Tomcat is working, follow these steps. (Please note that the links below only work if Tomcat is installed on your PC, running, and the port is 8080.)
In your Tomcat5 directory in the U: drive, or in the installation directory on your home PC, there is a conf directory that contains the configuration files that govern the default behavior of Tomcat. The two files we will modify in the beginning are server.xml and web.xml.
The only change that needs to be made to server.xml is to allow Tomcat
to automatically reload a servlet if its .class file has been modified.
Open server.xml in a text editor and look for the
closing tag. Add this code BEFORE the
</host>
tag.
<DefaultContext reloadable="true" />
The only change to make to the web.xml file is to uncomment the code for the invoker servlet. The invoker servlet allows Tomcat to execute any servlet that is in a directory. Without the invoker servlet, you would need to add information about every servlet to be run in the web.xml file of that directory. Open web.xml in a text editor, look for the following servlet definition, and uncomment it. If this code is not present, then add an uncommented version of it where all the other servlet definitions are.
<!--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-->
Look for the following servlet mapping and uncomment it. If this code is not present, then add an uncommented version of it where all the other servlet definitions are.
<!-- <servlet mapping> <servlet-name>invoker</servlet-name> <url-pattern>/servlet/*</url-pattern> </servlet-mapping> -->
After making changes to the configuration files, you will need to stop Tomcat and then start it again.
The default installation has a directory of servlets. The servlets will be
in
U:/tomcat/webapps/examples/WEB-INF/classes
Try to modify the HelloWorldExample
servlet. Edit the .java
file and add some text after the </h1>
, but before
the closing quote. Compile the file and be sure that the new .class
file is in the classes directory. It takes a little while for Tomcat
to recognize that the .class file has changed. Access the servlet
at
http://localhost:8080/examples/servlet/HelloWorldExample.
If you don't see the new phrase, wait another 30 seconds and reload it.
Eventually you will see the new change. You can look in the command shell
window that Tomcat opens when it is running. You can see when the servlet
has been reloaded.
Web Applications are directories that have a specific structure and contain some required files.
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> </web-app>
All Web Applications have this basic structure.
The next step is to let the servlet engine know that this application exists by adding a context for it.
<Context path="/your-webapp-dir" docBase="/full/path/to/your-webapp-dir" reloadable="true" crossContext="true" debug="0"/>
Now you can create a servlet in the your-webapp-dir directory and execute it through Tomcat. Copy amd paste the following into a new file named HelloServlet.java file and compile it. Place the .class file in the your-webapp-dir/WEB-INF/classes directory.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Simple servlet used to test server. * <P> * Taken from Core Servlets and JavaServer Pages 2nd Edition * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * © 2003 Marty Hall; may be freely used or adapted. */ public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE>Hello</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1>Hello</H1>\n" + "</BODY></HTML>"); } }
The URL for accessing this would
be
http://localhost:8080/your-webapp-dir/servlet/HelloServlet