Tomcat three simple website deployment methods

Tomcat service deployment

1. Implicit deployment

Why it can be deployed implicitly is because Tomcat is configured with a default host corresponding to application webapps . You can also configure other hosts under Engine (make sure the host you configure has a corresponding relationship under ect/host), but the appBase must be unique. Ensure that the resources under each host are isolated and will not interfere with each other. As follows:

Tip: The host mentioned here is called domain name in other words. localhost is the domain name of 127.0.0.1, and otherlocalhost is also a domain name of 127.0.0.1.
For example, the appBase directory corresponding to the localhost domain name is the default webapps, and webapps corresponds to n multiple website applications.
When you access using http://localhost:8080, Tomcat knows that you want to access a website in the webapps directory.
When you use http://otherlocalhost:8080 to access, Tomcat knows that you want to access a website in the otherHostWeb directory.

<Server port="8005" shutdown="SHUTDOWN">
    <Service name="Catalina">
    	<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    	<Engine name="Catalina" defaultHost="localhost">
    		<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
    		<Host name="otherlocalhost"  appBase="otherHostWeb" unpackWARs="true" autoDeploy="true">
    	</Engine>
	</Service>
</Server>

Suppose now I have a website MyWeb1 , which has an abc.html page with the following content:

<h1>Hello World11111!!!</h1>

Now you are ready to deploy MyWeb1 to the Tomcat server. You can directly drop the MyWeb1 website under the default host webapps . As shown below:

Insert image description here

Question: How to access the website under the webapps host?

Because the path access prefix is ​​not specified under the webapps host , the default is the prefix. Then which resource file under webapps you want to access , just locate the path directly, for example: abc.html page, access path:/http://localhost:8080/MyWeb1/abc.htmlThat’s it. If there are homepage index.html, index.jsp, etc. under the MyWeb1 website , access them directly.http://localhost:8080/MyWeb1You will be able to enter the home page. In the same way, the same is true for accessing other websites under the webapps host.

  • For example, the docs website, access path:http://localhost:8080/docs
  • For example, the manager website, access path:http://localhost:8080/manager
  • For example, the host-manager website, access path:http://localhost:8080/host-manager
  • For example, the examples website, access path:http://localhost:8080/examples

Finally, let me remind you that each website will have its own service processing Servlet, and these Servlets are integrated through WEB-INF/web.xml.

Implicit deployment is a bit unfriendly because the website application must be placed on the webapps host (when a host is configured). If there are too many files on this host, it is easy to make errors.

2. Display deployment

Server.xmlJust configure the Context website application directly in the file , as follows:

<Server port="8005" shutdown="SHUTDOWN">
    <Service name="Catalina">
    	<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    	<Engine name="Catalina" defaultHost="localhost">
    		<Context path="/myWeb1" docBase="/Users/gwm/Downloads/MyWeb1" reloadable="true">
    	</Engine>
	</Service>
</Server>
  • appBase: Equivalent to the root directory of a host /, the default name is webapps
  • path: There will be multiple websites under the webapps host, which can be distinguished by path.
  • docBase: Access the specific directory of website files. For example, the abc.htmlpath above is in the /Users/gwm/Downloads/MyWeb1 file directory. Because docBase can specify the location of website resource files, you do not need to put the website under the previous webapps host.

So the final path to access abc.html is:http://localhost:8080/myWeb1/abc.html

3. xml configuration deployment

The above shows that deployment requires modification of Server.xmlthe global configuration file. This file is very important, generally do not modify it easily, otherwise it will not start. So here is another deployment method, which is also the most commonly used method - xml configuration method .

After you run Tomcat once, Tomcat will automatically conf generate /config/Catalina/localhost/a directory under the file directory. Then you can create a new xml file in this directory. The name of the xml file is the access path to your website. For example, create a new abc.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/Users/gwm/Downloads/MyWeb1" reloadable="true"></Context>

Warning: Note here that when declaring the xml configuration, there must be no blank line above the first line. Otherwise, this xml file will not take effect.

The overall directory is shown below:

Insert image description here

Final access path:http://localhost:8080/abc/abc.htmlIt is recommended to use the third method to deploy services. The modified files are all partial files and will not affect the deployment of others.

Guess you like

Origin blog.csdn.net/qq_35971258/article/details/128704491
Recommended