This chapter describes how to create and publish Web applications on Tomcat. The following describes the directory structure of Tomcat and the directory structure of Web applications. Next, describes how to deploy HTML, Servlet, JSP, and tag library to Web applications. The Web application packaging and publishing method finally introduces how to configure a virtual host on Tomcat.
This chapter focuses on the structure and release method of Web applications. Therefore, the servlet and jsp examples in this chapter are not explained in detail. For details about Servlet and JSP technologies, refer to the contents in other chapters.
2.1 Tomcat directory structure
Before publishing a web application on Tomcat, you must first understand the directory structure of Tomcat. For the directory structure of Tomcat, see Table 2-1. .
Table 2-1 Tomcat directory structure
Contents |
Description |
/Bin |
Scripts for starting and disabling Tomcat on Windows and Linux platforms |
/Conf |
Stores various configuration files of the Tomcat server. The most important configuration file is server. xml. |
/Server |
Contains three subdirectories: classes, Lib, and webapps. |
/Server/lib |
Store various jar files required by the Tomcat server |
/Server/webapps |
Stores two built-in Tomcat web applications: the admin application and the Manager application. |
/Common/lib |
Stores jar files accessible to Tomcat servers and all web applications. |
/Shared/lib |
Stores jar files accessible to all web applications |
/Logs |
Store Tomcat log files |
/Webapps |
When a web application is published, the Web application file is stored in this directory by default. |
/Work |
Tomcat stores the servlet generated by JSP in this directory |
From table 2-1, we can see that jar files can be stored in the/Server/lib directory,/common/lib directory, and/shared/lib directory. Their differences are as follows:
Jar files in the/Server/lib directory can only be accessed by the Tomcat server
Jar files in the/shared/lib directory can be accessed by all web applications, but cannot be accessed by the Tomcat server.
Jar files in the/common/lib directory can be accessed by the Tomcat server and all web applications.
In addition, for the Java Web application that will be introduced below, under its WEB-INF directory, you can also establish the Lib subdirectory, under the Lib subdirectory can put a variety of jar files, these jar files can only be accessed by the current web application.
In the lib directory mentioned above, only jar files are accepted. If the class compression file is a zip file, expand it, repackage it as a jar file, and copy it to the lib directory. If you copy the ZIP file directly to the lib directory, you will find that the Tomcat server still cannot find the relevant classes. For Packaging Commands, see Section 2.2.8.
2.2 create and publish Web Applications
Java Web applications consist of a set of Static HTML pages, Servlets, JSP and other related classes. Each component has a fixed storage directory in the Web application. The configuration information of the Web application is stored in the web. xml file. When publishing certain components (such as servlets), you must add the corresponding configuration information to the Web. xml file.
2.2.1 directory structure of Web Applications
The Web application has a fixed directory structure. Here we assume that a Web application named helloapp is developed. First, you should /Create the directory structure of the web application under the webapps directory. See table 2-2.
Table 2-2 directory structure of Web Applications
Contents |
Description |
/Helloapp |
The root directory of the Web application. All JSP and HTML files are stored in this directory. |
/Helloapps/WEB-INF |
Stores the web application release description file web. xml |
/Helloapp/WEB-INF/classes |
Stores various class files. servlet class files are also stored in this directory. |
/Helloapp/WEB-INF/lib |
Various jar files required for storing Web Applications |
For example, you can store the JAR file of the JDBC driver in this directory.
From table 2-2, we can see that both the classes and Lib sub-directories can store Java class files. During running, Tomcat's class loader first loads classes under the classes directory, and then classes under the lib directory. Therefore, if two directories have classes of the same name, the classes in the classes Directory have priority.
The directory structure of the helloapp application introduced in this chapter is 2-1, and the helloapp application is expanded in Windows Resource Manager 2-2.
The following components are created in the helloapp application:
HTML component: index.htm
JSP components: Login. jsp and hello. jsp
Servlet component: dispatcherservlet
The links between these components are:
Index.htm → login. jsp → dispatcherservlet → hello. jsp
Figure 2-1 directory structure of the helloapp Application
Figure 2-2 show the helloapp application in Windows Resource Manager
2.2.2 web. xml file
A Java Web application configures its publishing information through an XML-based release descriptor file named Web. XML, which is stored in the WEB-INF subdirectory. In appendix B, the configuration method of Web. XML is described in detail. In appendix C, the basic knowledge of XML is introduced. The following configuration information can be contained in the web. xml file:
Servlet Definition
Servlet initialization parameters
Servlet and JSP ing
Security domain configuration parameters
List of welcome files
Resource reference
Definition of Environment Variables
Now, create a default web. xml file and put it in the WEB-INF directory.
<?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/ j2ee/dtds/web-app_2_3.dtd'><web-app></web-app> |
The first line of the web. xml file specifies the XML version and character encoding, and the second line of doctype specifies the document type. Next, All configuration elements of the web application will be added to this Element.
2.2.3 add in server. xml Element
Element is /CONF/server. XML is the most frequently used element. A single web application. One Can have multiple Element. Each web application must have a unique URL path. The path attribute of the element. For example Add the following to the element: Element:
<!-- Define the default virtual host --><Host name="localhost" debug="0" appBase="webapps"unpackWARs="true" autoDeploy="true">…………<Context path="/helloapp"docBase="helloapp" debug="0"reloadable="true"/></Host> |
For details about the attributes of the context element, see table 2-3.
Table 2-3 attributes of the context element
Genus |
Description |
Path |
URL entry for accessing the Web Application |
Docbase |
Specify the file path of the Web application. You can specify the absolute path or the relative path of the appbase attribute relative to the host (for details about the appbase attribute of the host, see Section 2.3 ). If the web application adopts an open directory structure, specify the root directory of the Web application. If the web application is a war file, specify the path of the war file. |
Reloadable |
If this property is set to true, the Tomcat server will monitor changes to the class files in the WEB-INF/classes and WEB-INF/lib directories while running. If a class file is updated, the server automatically reloads the web application. |
In the development phase, setting the reloadable attribute to true is helpful for debugging Servlet and other class files. However, this feature will increase the load on the server. Therefore, we recommend that you set this attribute to false in the Web application product release phase.