web| detailed 2.2.8 Create and publish a war file
Tomcat can run either a Web application that uses an open directory structure or a war file. All source files are available in the Sourcecode/chapter2/helloapp directory of the CD-ROM, as long as the entire HelloApp directory is copied to /webapps directory, you can run Open directory structure of the HelloApp application. In the development phase of Web applications, an open directory structure is often used to publish Web applications for ease of debugging, which makes it easy to update or replace files. If the development is complete, enter the product release phase, the entire Web application should be packaged as a war file, and then published.
In this case, publish the HelloApp as follows.
(1) Access to the root directory of HelloApp application /webapps/helloapp.
(2) package the entire Web application as a Helloapp.war file with the following commands:
Packaged program Jar.exe is provided in the JDK's Bin directory. If you want to expand the Helloapp.war file, the command is:
(3) Copy the Helloapp.war file to The/webapps directory.
(4) Delete the original HelloApp directory.
(5) Start the Tomcat server.
When the Tomcat server is started, all war files in the WebApps directory are automatically expanded to an open directory structure. So when the server starts, it will find that the server has Helloapp.war expanded to The/webapps/helloapp directory.
2.3 Configuring the Virtual host
In the Tomcat configuration file server.xml, the host element represents a virtual host, and multiple virtual hosts can be configured under the same engine element. For example, there are two companies whose Web applications are posted on the same tomcat server, and each company can create a separate virtual host with the following virtual host names:
This way, when a Web customer accesses more than two Web applications, it is as if the two applications have their own hosts. In addition, you can create aliases for a virtual host, for example, if you want the Web customer access www.mycompany1.com or mycompany1.com to be able to connect to the same Web, you can treat the mycompany1.com as a virtual host alias.
The following explains how to configure the www.mycompany1.com virtual host.
(1) Open /conf/server.xml file, you will find There is already an element named LocalHost in the element that can be in the back of it (that is,Later) Add the following Elements:
The above configuration code is located in the Sourcecode/chapter2/virtualhost-configure.xml file of this book's companion CD.
The attribute description of the element is shown in table 2-5:
Table 2-5 Attributes of an element
||Specify the name of the virtual host
||Specify Log level
||Specifies the directory for the virtual host, either by specifying an absolute directory or by specifying the relative relative directory. If this item is not set, the default value is /webapps
||If this key is set to True, the Web application's war file will be expanded to an open directory structure before running. If set to False, the war file is run directly
||If this key is set to true, it means that when the Tomcat server is running, it is able to monitor the files under AppBase, and if a new Web application is added, the Web application is automatically published
||Specify the alias of the virtual host, you can specify multiple aliases
||If this key is set to true, it means that all Web applications under the AppBase directory are automatically published by the Tomcat server when it is started. If the Web application does not have a corresponding element in Server.xml, the default context configuration is used. The default value of Deployonstartup is true
In The Deployonstartup property is true, even if you do not join the HelloApp application in Server.xml element, the Tomcat server can also automatically publish and run HelloApp applications.
In this case, Tomcat uses the default defaultcontext. The knowledge about Defaultcontext can refer to the Tomcat documentation:
(2) Copy the HelloApp application (Helloapp.war file or the entire HelloApp directory) to the AppBase property specified directory C:\mycompany1.
(3) In order for the above configured virtual hosts to take effect, the above virtual hostname and alias must be registered in the DNS server so that their IP address points to the machine on which the Tomcat server resides. The following names must be registered:
(4) Restart the Tomcat server, and then access through the browser: http://www.mycompany1.com/helloapp/index.htm, if returned to the normal page to indicate that the configuration was successful. HelloApp applications can also be accessed through the alias of the virtual machine:
This chapter introduces the steps for creating and publishing Web applications on Tomcat by using the HelloApp Web application example. Through this chapter, readers can learn to create a directory structure for Web applications, create web.xml files, and deploy HTML, Servlet, JSP, and tag library to Web applications.
In addition, readers can learn how to package and publish an entire Web application. This chapter also describes how to configure a virtual host. In order to facilitate the reader to compile the source program, in this book companion CD-ROM Sourcecode/chapter2 directory provides the compilation of this chapter Java program Script Compile.bat, its contents are as follows:
Set Catalina_home=c:\jakarta-tomcatset path=%path%; C:\j2sdk1.4.2\binset currpath=.\if "%os%" = "windows_nt" Set Currpath=%~dp0%set src=%currpath%helloapp\srcset dest=% Currpath%helloapp\web-inf\classesset classpath=%catalina_home%\common\lib\servlet-api.jar; %catalina_home%\common\lib\jsp-api.jarjavac-sourcepath%src%-D%dest% %src%\mypack\ Dispatcherservlet.javajavac-sourcepath%src%- d%dest% %src%\mypack\hellotag.java
When running this script, just reset the directory of the Tomcat directory and JDK above.
In the Javac command,-sourcepath sets the path to the Java source file,-D sets the path of the class that compiles the build. The-classpath parameter of the JAVAC command can set the classpath path, and if it is not set, reference the settings of the environment variable CLASSPATH.
This article extracts the free Flying Ideas Book licensing "Tomcat and Java Web Development technology detailed"