Вы находитесь на странице: 1из 6

Apache Tomcat - Tutorial

Lars Vogel
Version 0.5 Copyright 2008 - 2010 Lars Vogel 03.08.2010 Revision History Revision 0.1 Created Revision 0.2-0.5 bug fixed and enhancements Apache Tomcat This article describes the installation and usage of Apache Tomcat for Java web development. Tomcat version 6.x is used in this tutorial. 09.04.2009 - 03.08.2010 Lars Vogel 18.11.2008 Lars Vogel

Table of Contents 1. Apache Tomcat 2. Installation 2.1. Windows 2.2. Ubuntu Linux 2.3. Admin user 3. Managing Apache Tomcat 3.1. Start Tomcat on Windows 3.2. Start Tomcat on Ubuntu (Linux) 3.3. Test Tomcat 3.4. Deployment 4. Security Manager 5. Developing Java web applications 6. Tomcat as HTTP Server 7. Thank you 8. Questions and Discussion 9. Links and Literature 9.1. Apache Tomcat

1. Apache Tomcat
Apache Tomcat is a webcontainer which allows to run servlet and JavaServer Pages based web applications. Most of the modern Java web frameworks are based on servlets and JavaServer Pages and can run on Apache Tomcat, e.g. JavaServer Faces, Struts, Spring. Apache Tomcat also provides per default a HTTP connector on port 8080, e.g. Tomcat can also be used as HTTP server even though the performance of Tomcat is not as good as the performance of the Apache HTTP server.

2. Installation
2.1. Windows
Download the Windows installer for Tomcat6 from Apache Tomcat Homepage and run the installer. The installation directory of Tomcat looks like the following:

2.2. Ubuntu Linux


For Ubuntu run via sudo the following commands.
apt-get apt-get apt-get apt-get install install install install tomcat6 tomcat6-admin tomcat6-docs tomcat6-examples

2.3. Admin user

The default user under windows for tomcat is "admin" with the password "admin". The maintained users can be found in the tomcat installation directory /conf/tomcat-users.xml On Ubuntu the admin user is usually not created automatically, you have to add the user entry manually to /etc/tomcat6/tomcat-users.xml. Check http://localhost:8080/manager/html for the required user in the error message.

3. Managing Apache Tomcat


3.1. Start Tomcat on Windows
To start Tomcat use "tomcat6.exe" in the bin directory. Alternatively you can use a graphical userinterface to start Tomcat. From your menu select "Configure Tomcat".

To start Tomcat directly press the button "Start". Tomcat can get configured to started automatically via the "Automatic" selection in the drop-down "Startup type".

3.2. Start Tomcat on Ubuntu (Linux)


In Ubuntu Tomcat is automatically started. To restart tomcat use the command sudo /etc/init.d/tomcat6 restart

3.3. Test Tomcat


The default port for Tomcat is 8080. After starting Tomcat on your local you can validate if Tomcat is running the URL: http://localhost:8080.

3.4. Deployment
The standard deployment format for webapplications is .war. If you have create a war application just put this application into the "webapps" folder. The next time tomcat starts it will unpack the war and make the application available. Web applications may require external libraries.Typically web application contain there own libraries but if you want to make certain libraries avaiable for all applications you can put them

into the folder "lib" and a subfolder below "lib". These libraries are then available for all web applications.

4. Security Manager
Tomcat uses the same security model as Java. The default security manager configured in Tomcat will for example prevent the webapplication to write to the file system. This security manager is configured in the catalina.policy file in the conf directory. For details on the manager see http://tomcat.apache.org/tomcat-6.0-doc/security-managerhowto.html . If you want for example to grand the webapplication "performance" access to all files in the server directory c:\temp\wstest then add the following entry to "catalina.policy".
grant codeBase "file:${catalina.home}/webapps/performance/-" java.io.FilePermission "c:\temp\wstest\<<ALL FILES>>", "read,write,execute,delete"; }; {

5. Developing Java web applications


After going through the setup you properly want to learn how to develop servlets and JSP on Tomcat. Please see Servlets and JSP development - Tutorial

6. Tomcat as HTTP Server


Apache Tomcat contains also a HTTP connector which can be used to serve static HTML pages. The standard directory which will be served by Apache Tomcat is below the Tomcat installation directory "webapps/ROOT". Place static content which should be served by Tomcat into this directory.

Tip
To allow directory browsing via Apache Tomcat change the parameter "listings" in the file conf/web.xml from false to true.
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servletclass> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>true</param-value>

</init-param> <load-on-startup>1</load-on-startup> </servlet>

7. Thank you
Please help me to support this article:

8. Questions and Discussion


Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

9. Links and Literature


9.1. Apache Tomcat
http://tomcat.apache.org/ Apache Tomcat Homepage Java Webdevelopment with Eclipse WTP - Tutorial http://wiki.eclipse.org/WTP_Tomcat_FAQ Eclipse WTP and Tomcat FAQ http://tomcat.apache.org/tomcat-6.0-doc/security-manager-howto.html Apache Tomcat Sercurity Manager How-to

Вам также может понравиться