Apache Tomcat
Apache Tomcat provides a "pure Java" HTTP web server environment in which Java code can also run. Thus it is a Java web application server.
Install & Run
Download Tomcat 10 at https://tomcat.apache.org/download-10.cgi
-
Download file
apache-tomcat-10.1.18.zip
-
unzip to
apache-tomcat-10.1.18
-
cd
apache-tomcat-10.1.18/bin
-
run
./startup.sh
orstartup.bat
for Windows -
visit http://localhost:8080/
-
shutdown
./shutdown.sh
ENV
$CATALINA_HOME
:/path/to/apache-tomcat-10.1.18
$CATALINA_BASE
: will be set to the value ofCATALINA_HOME
by default
Deployment
Tomcat can be used to deploy Java web application.
-
Copy the web application archive file (
*.war
) into directory$CATALINA_BASE/webapps/
. When Tomcat is started, it will automatically expand the web application archive file into its unpacked form, and execute the application that way. -
Copy
unpacked
directory hierarchy into a subdirectory in directory$CATALINA_BASE/webapps/
. Tomcat will assign acontext path
to application based on thesubdirectory name
. -
Use Tomcat "Manager" or Tomcat Deployer
If $CATALINA_BASE/webapps/
has demo-app.war
or demo-app/
, then you can access your application at http://localhost:8080/demo-app
Standard Directory Layout
Tomcat uses the same layout as WAR format. Root directory
-
*.html, *.jsp, etc - The HTML and JSP pages, along with other files that must be visible to client browser (such as JavaScript, CSS, and images).
-
/WEB-INF/web.xml - The Web Application Deployment Descriptor.
-
/WEB-INF/classes/ - Java class files and resources, including both servlet and non-servlet classes, that are not combined into JAR files. Java packages will be reflected as directory.
For example, a Java class named
com.mycompany.mypackage.MyServlet
would need to be stored in a file named/WEB-INF/classes/com/mycompany/mypackage/MyServlet.class
. -
/WEB-INF/lib/ - JAR files that contain Java class files and resources, such as third party class libraries or JDBC drivers.
Shared Library Files
$CATALINA_HOME/lib
library JAR files (or unpacked classes) are visible to both web applications and internal Tomcat code.
Web Application Deployment Description
/WEB-INF/web.xml defines everything about your application that a server needs to know (except the context path, which is assigned by the system administrator when the application is deployed).
A basic `web.xml` file
```xml<!-- General description of your web application -->
<display-name>My Web Application</display-name>
<description>
This is version X.X of an application to perform
a wild and wonderful task, based on servlets and
JSP pages. It was written by Dave Developer
(dave@mycompany.com), who should be contacted for
more information.
</description>
<!-- Context initialization parameters that define shared
String constants used within your application, which
can be customized by the system administrator who is
installing your application. The values actually
assigned to these parameters can be retrieved in a
servlet or JSP page by calling:
String value =
getServletContext().getInitParameter("name");
where "name" matches the <param-name> element of
one of these initialization parameters.
You can define any number of context initialization
parameters, including zero.
-->
<context-param>
<param-name>webadmin</param-name>
<param-value>myaddress@mycompany.com</param-value>
<description>
The EMAIL address of the administrator to whom questions
and comments about this application should be addressed.
</description>
</context-param>
<!-- Servlet definitions for the servlets that make up
your web application, including initialization
parameters. With Tomcat, you can also send requests
to servlets not listed here with a request like this:
http://localhost:8080/{context-path}/servlet/{classname}
but this usage is not guaranteed to be portable. It also
makes relative references to images and other resources
required by your servlet more complicated, so defining
all of your servlets (and defining a mapping to them with
a servlet-mapping element) is recommended.
Servlet initialization parameters can be retrieved in a
servlet or JSP page by calling:
String value =
getServletConfig().getInitParameter("name");
where "name" matches the <param-name> element of
one of these initialization parameters.
You can define any number of servlets, including zero.
-->
<servlet>
<servlet-name>controller</servlet-name>
<description>
This servlet plays the "controller" role in the MVC architecture
used in this application. It is generally mapped to the ".do"
filename extension with a servlet-mapping element, and all form
submits in the app will be submitted to a request URI like
"saveCustomer.do", which will therefore be mapped to this servlet.
The initialization parameter names for this servlet are the
"servlet path" that will be received by this servlet (after the
filename extension is removed). The corresponding value is the
name of the action class that will be used to process this request.
</description>
<servlet-class>com.mycompany.mypackage.ControllerServlet</servlet-class>
<init-param>
<param-name>listOrders</param-name>
<param-value>com.mycompany.myactions.ListOrdersAction</param-value>
</init-param>
<init-param>
<param-name>saveCustomer</param-name>
<param-value>com.mycompany.myactions.SaveCustomerAction</param-value>
</init-param>
<!-- Load this servlet at server startup time -->
<load-on-startup>5</load-on-startup>
</servlet>
<servlet>
<servlet-name>graph</servlet-name>
<description>
This servlet produces GIF images that are dynamically generated
graphs, based on the input parameters included on the request.
It is generally mapped to a specific request URI like "/graph".
</description>
</servlet>
<!-- Define mappings that are used by the servlet container to
translate a particular request URI (context-relative) to a
particular servlet. The examples below correspond to the
servlet descriptions above. Thus, a request URI like:
http://localhost:8080/{contextpath}/graph
will be mapped to the "graph" servlet, while a request like:
http://localhost:8080/{contextpath}/saveCustomer.do
will be mapped to the "controller" servlet.
You may define any number of servlet mappings, including zero.
It is also legal to define more than one mapping for the same
servlet, if you wish to.
-->
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>graph</servlet-name>
<url-pattern>/graph</url-pattern>
</servlet-mapping>
<!-- Define the default session timeout for your application,
in minutes. From a servlet or JSP page, you can modify
the timeout for a particular session dynamically by using
HttpSession.getMaxInactiveInterval(). -->
<session-config>
<session-timeout>30</session-timeout> <!-- 30 minutes -->
</session-config>
```
Tomcat Context Descriptor
A /META-INF/context.xml file can be used to define Tomcat specific configuration options, such as an access log, data sources, session manager configuration and more. See Tomcat configuration documentation