Tomcat and Java Web Development technology in one of the serial

Source: Internet
Author: User
Tags add definition version zip java web root directory tomcat tomcat server
Web| detailed This chapter describes how to create and publish Web applications on Tomcat. Let's start with the directory structure of Tomcat and the directory structure of Web applications, then explain how to deploy HTML, Servlet, JSP, and tag library to a Web application, and then introduce ways to package and publish the entire Web application. Finally, it describes how to configure a virtual host on Tomcat.

This chapter focuses on the structure and publishing methods of Web applications, so there is no detailed explanation of the servlet and JSP examples in this chapter, and the techniques for servlet and JSP can be referenced separately from the other chapters.

Directory Structure of 2.1 tomcat

Before you publish a Web application on Tomcat, first understand the directory structure of Tomcat. The directory structure of Tomcat see table 2-1, these directories are Subdirectories.

Table 2-1 Tomcat directory structure

Directory Description
/bin Script file to start and close Tomcat on Windows platform and Linux platform
/conf A variety of configuration files for the Tomcat server, with the most important profile being Server.xml
/server Contains 3 subdirectories: Classes, lib, and WebApps
/server/lib The various jar files needed to store the Tomcat server
/server/webapps Two Web applications hosted by Tomcat: Admin Application and Manager application
/common/lib Storage of a Tomcat server and jar files accessible to all Web applications
/shared/lib Store jar files that are accessible to all Web applications
/logs Log files that store tomcat
/webapps When publishing a Web application, the Web application file is placed in this directory by default
/work Tomcat puts the JSP-generated servlet in this directory


As you can see from table 2-1, jar files can be placed in the/server/lib directory,/common/lib, and/shared/lib directories, and the difference is:

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 not by the Tomcat server

Jar files in the/common/lib directory can be accessed by Tomcat server and all Web applications

In addition, for the Java Web application that will be described below, in its Web-inf directory, Lib subdirectories can be created, and various jar files are placed in Lib subdirectories, which can only be accessed by the current Web application.

Only jar files are accepted in the Lib directory mentioned above, if the class compressed file is a zip file, it should be expanded, repackaged as a jar file, and then copied into 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 related classes. Package command See section 2.2.8.

2.2 Creating and publishing Web applications

A Java Web application consists of a set of static HTML pages, Servlet, JSP, and other related classes. Each component has a fixed storage directory in the Web application. The configuration information for the Web application is stored in the Web.xml file. When you publish certain components, such as servlet, you must add the appropriate configuration information to the Web.xml file.

Directory structure for 2.2.1 Web applications

Web applications have a fixed directory structure, which assumes the development of a Web application named HelloApp. First, you should /WEBAPPS directory structure to create this Web application, see table 2-2.

Table 2-2 The directory structure of Web applications

Directory Description
/helloapp The root directory of Web applications, all JSP and HTML files are stored in this directory
/helloapp/web-inf Publish description file for Web application Web.xml
/helloapp/web-inf/classes Store a variety of class files, servlet class files are also placed in this directory
/helloapp/web-inf/lib The various jar files needed to store the Web application


For example, in this directory, you can store the JDBC driver's jar file

From table 2-2, we see that Java class files can be stored in classes and Lib subdirectories. During the run, the Tomcat class loader first loads the classes in the classes directory and then loads the classes in the Lib directory. Therefore, if a class with the same name exists under two directories, the class in the classes directory has precedence.

The directory structure of the HelloApp application described in this chapter is shown in Figure 2-1, and HelloApp is expanded in Windows Explorer as shown in Figure 2-2.

The following components were created in the HelloApp application:

HTML components: index.htm

JSP components: login.jsp and hello.jsp

Servlet components: Dispatcherservlet

The link relationships between these components are:

index.htm→login.jsp→dispatcherservlet→hello.jsp



Figure 2-1 HelloApp Application directory structure



Figure 2-2 HelloApp application in Windows Explorer expansion diagram

2.2.2 Web.xml File

The Java Web application configures its publishing information with an xml-based publishing descriptor file named Web.xml, which is stored in the Web-inf subdirectory. The Web.xml configuration method is described in detail in Appendix B, and the basics of XML are described in Appendix C. The following configuration information can be included in the Web.xml file:

The definition of a servlet

Initialization parameters for servlet

The servlet and JSP mappings

Security Domain Configuration Parameters

Welcome file List

Resource reference

Definition of environment variables

Now, create a default Web.xml file and put the file 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  java.sun.com/  j2ee/dtds/web-app_2_3.dtd ' ><web-app></web-app>
       
          


The first line of the above Web.xml file specifies the version and character encoding of the XML, the second line DOCTYPE specifies the document type, and then declares a element, all configuration elements about the Web application are added to this Element.

2.2.3 joined in the Server.xml Elements

Element is The most frequently used element in the/conf/server.xml, which represents running in the A single Web application on. One can have multiple Elements. Each Web application must have a unique URL path, the URL path in the element is set in the path attribute. For example, in a named "localhost" element, add the following Elements:

       
                     
                      
        <!--Define The default virtual host-->


A description of the individual properties of the context element is shown in table 2-3.

Table 2-3 The properties of a context element

Property Description
Path Specify the URL portal to access the Web application
DocBase Specifies the file path for the Web application, either given an absolute path or a relative path relative to the host's AppBase property (see section 2.3 for the host's AppBase property). Specifies the root directory of the Web application if the Web application uses an open directory structure, and specifies the path to the war file if the Web application is a war file
Reloadable If this property is set to True, the Tomcat server will monitor changes to the class file in the Web-inf/classes and Web-inf/lib directories under run state. If a class file is detected and updated, the server automatically reloads the Web application


In the development phase, setting the Reloadable property to True helps debug the servlet and other class files. However, because this functionality can aggravate the server's running load, it is recommended that you set this property to False in the product publishing phase of the Web application.


This article extracts the free Flying Ideas Book licensing "Tomcat and Java Web Development technology detailed"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.