Tomcat and Java Web Development technology in detail serialization of the third

Source: Internet
Author: User
Tags aliases copy key java web access root directory tomcat tomcat server
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:

       
          
           
        Jar CVF  Helloapp.war  *.*
       
          


Packaged program Jar.exe is provided in the JDK's Bin directory. If you want to expand the Helloapp.war file, the command is:

       
          
           
        Jar  XVF  Helloapp.war
       
          


(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:

       
            
             
        Www.mycompany1.comwww.mycompany2.com
       
            


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

Property Description
Name Specify the name of the virtual host
Debug Specify Log level
AppBase 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
Unpackwars 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
Autodeploy 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
Alias Specify the alias of the virtual host, you can specify multiple aliases
Deployonstartup 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:

/webapps/tomcat-docs/config/defaultcontext.html

(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:

       
                      
                       
        Www.mycompany1.commycompany1.commycompany1
       
                      


(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:

Http://mycompany1.com/helloapp/index.htm

Http://mycompany1/helloapp/index.htm

2.4 Summary

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"

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.