A summary of the Tomcat deployment Web application approach

Source: Internet
Author: User
Tags file url

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:<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 when the content in the application changes, this property is usually set to true during the development phase, is easy to develop, should be set to false in the release phase, improve the application's visit Ask for Speed.

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 location of the cache file, refers to the Tomcat parsing JSP converted to Java files, and compiled into a class storage folder, set in the project folder, you can avoid porting to other places the first time to read the JSP file needs to be re-parsed. General Format: project folder/work

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

This is similar to Method 2, but instead of adding a context tag to the Server.xml file, add an XML file, such as Pet.xml, to the $catalina_home/conf/catalina/localhost.

In the Tomcat installation directory Conf/catalina/localhost, there are three apps from Tomcat, feel free to copy one of the XML files, then modify Docbase to point to your own application and change the file name to

The parameters of the <Context> tag in Method 2, or you can create a new XML file yourself. ( Note that this file name is used as the Path property value in the context, regardless of how the Path property value in the file is set

is also invalid ), 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 is a Web thread

All contents of the sequence are compressed.

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 the last one.) ”)。 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 service.

The Pet.war can be deployed 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 use the following methods to take

Auto-unzip, set the Unpackwar property in the XML configuration file to "false".

Ii. Dynamic Deployment

dynamic deployment refers to the ability to deploy a Web application after the server starts without restarting the server. Dynamic deployment uses the Manager.war file provided by the server, if the $catalina_home/webapps/

Without the file, you have to re-download Tomcat, otherwise 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 with the following: (about

For more information on this file, please refer to Security model for Java 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 contents:<context reloadable = "false" / > . DocBase don't 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 the above has seen your Web application, the name is your context

Path (option): the 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": Avoid using Tomcat to deploy 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. And it has to be webapps/petweb/index.html.

You can access the Index.html page via http://localhost:8080/petweb/index.html.

A summary of the Tomcat 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.