A summary of the Tomcat learning------Deployment Web application approach

Source: Internet
Author: User
Tags file url

A summary of the Tomcat deployment Web application approach

There are two ways to deploy Java Web applications in Tomcat: Static deployment and dynamic deployment.

In the following $catalina_home refers to the Tomcat root directory.

First, static deployment

Static deployment means that we deploy our program before the server starts, and only after the server is started can our web application access it.

The following 3 ways can be deployed: (Take the Petweb project as an example, the Petweb directory assumes f:/petweb)

1. using Tomcat to deploy automatically

Copy the Petweb directory to $catalina_home/webapps and start the server, and the app will be loaded automatically when Tomcat starts.

The access address is as follows:http://localhost:8080/PetWeb/

This is a simpler approach, but the Web application must be in the WebApps directory. Tomcat's WebApps directory is the default app directory for Tomcat and will load all applications in this directory when the server is started.

2. Modify the Server.xml file deployment

This way, you can not copy the Petweb directory to WebApps and deploy it directly in f:/. Here's how to change the $catalina_home/conf/server.xml file,

Find the following:

XML code:

    1. <context path ="/pet" reloadable ="false" docBase ="F:/petweb" workdir ="D:/mywebapps/emp" />

Path: is the root address of the access, indicating the path of the access, as in the above example, access to the application address is as follows:http://localhost:8080/Pet/

Reloadable: Indicates that the class package can be loaded automatically at run time under the classes and Lib folders. where reloadable= "false" means that the server does not automatically load after the content in the application is changed, and this property is usually set to true during the development phase, which is easy to develop, and should be set to false in the release phase to increase the access speed of the application.

Docbase: Represents the path to the application, note the direction of the slash "/". Docbase can use absolute paths, or relative paths, relative to WebApps.

Workdir: Represents the drop address of the cache file

3. Add custom Web Deployment files ( recommended, no need to restart Tomcat)

This approach is similar to Method 2, but instead of adding a context tag to the Server.xml file, add an XML file to the $catalina_home/conf/catalina/localhost. such as Pet.xml. Under the Tomcat installation directory Conf/catalina/localhost, there are three apps that Tomcat comes with, feel free to copy one of the XML files, then modify Docbase to point to your own application and rename the file name, Each parameter is described in the parameters of the <Context> tag in Method 2, or you can create a new XML file yourself. ( Note that this file name will be used as the Path property value in the context, regardless of how the Path property value is set in the file), copy the following and modify the appropriate path.

XML code:

    1. <context path ="/pet" docBase ="F:/petweb"
    2. debug ="0" privileged ="true" reloadable ="false" >
    3. </Context>

The access address is as follows:http://localhost:8080/Pet/

Note: Web apps are deployed as. War files

The JSP program can be packaged into a war package placed in the directory, the server will automatically unlock the war package, and in this directory to generate a folder with the same name. A war package is a jar package with an attribute format that compresses all the contents of a Web program.

We just deployed the Petweb folder on the server, and we know that we can make the content of the Web application into a . War package, and then deploy it on the server. For packaging please refer to the following steps:
1. Open command Prompt (cmd)
2. Setting the JDK environment variable
3. After entering the project folder F:/petweb at the command prompt, type the following command:jar CVF Pet.war */. (Note that there was a "." )。 This should have the Pet.war file under F:/petweb. (You can also package to the specified place, the command is as follows:jar CVF D:/pet.war */. )

Deploying the Pet.war file is as simple as changing the docbase= "F:/petweb" in the XML file to docbase= "F:/pet.war" or simply copying it to the WebApps directory. Then restart the server to deploy Pet.war as a Web application.

If you're careful, you'll find that the server Pet.war the file, and a pet folder is generated under WebApps, and the contents of the Pet.war are copied into it. We can cancel the automatic decompression by setting the Unpackwar property in the XML configuration file to "false" in the following way .

Ii. Dynamic Deployment

Dynamic deployment means that you can deploy a Web application after the server starts without restarting the server. Dynamic deployment to use the server provided by the Manager.war file, if there is no $catalina_home/webapps/under the file, you must go to re-download Tomcat, or you will not be able to complete the following functions. To use the hypervisor, you must first edit the $catalina_home/conf/tomcat-users.xml file, as follows: (For more on this file, refer to java Security model for Web applications two )

<tomcat-users >
<role rolename= "tomcat"/>
<role rolename= "Role1"/>
<role rolename= "manager"/>
<user username=" Coresun "password=" Coresun "roles=" manager "/>
<user username= "Tomcat" password= "Tomcat" roles= "Tomcat"/>
<user username= "both" password= "Tomcat" roles= "Tomcat,role1"/>
<user username= "Role1" password= "Tomcat" roles= "Role1"/>
</TOMCAT-USERS>

Then type the following address in the browser:http://localhost:8080/ , you should see a Garfield. Click the Tomcat Manager link on the left to prompt for your user name and password, this article is Coresun, and you can see the following pages:

(1) context Path (option): Enter/pet in


(2) in the XML configration file URL, you specify an. xml document, for example, we create a pet.xml file under f:/, with the following content:<context reloadable=" False " /> . docbase No need to write, because you want to fill in the next text box. or more simply, this text box doesn't fill anything.


(3) WAR or Directory URL: type F:/petwet or F:/pet.war, and then click the Deploy button to see if you have seen your Web application, the name is your context Path (option): Name in the.


(4) If you have a simpler way of deploying a. War file, here's a Select War file Upload click Browse to select the. war files, and then click Deploy.

Let Tomcat run only the web app specified in Conf/server.xml

There are 2 ways to do this:

Implement one:

1) Place the Web app you want to deploy in a path other than WebApps and specify Docbase in the corresponding context in Server.xml.

2) Delete all the folders in WebApps, and all the XML files under Conf/catalina/localhost.
Note: WebApps is the value of the AppBase property of the Host element in Server.xml.

Implementation two:

Modify the properties of the Host element in Server.xml, add or modify: deployxml="false" deployonstartup="false" autodeploy= "false"

Meaning:
deployxml="false": do not deploy XML corresponding web app under Conf/catalina/localhost

deployonstartup="false": when Tomcat starts, all Web apps under WebApps are not deployed

autodeploy="false": Prevent Tomcat from deploying the Web App under WebApps again when scanning for changes.

Note:

Tomcat in the WebApps directory can not be directly stored in a Web page format file, otherwise you cannot access the file, you must have a subdirectory to access the Web page file.
For example: We put index.html directly in the WebApps directory, http://localhost:8080/index.html through the browser is inaccessible to index.html. You must be webapps/petweb/index.html to access the index.html page through http://localhost:8080/petweb/index.html.

A summary of the Tomcat learning------Deployment Web application approach

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.