How can a Tomcat deploy multiple projects? With a variety of solutions and detailed steps!

This article comes from an interview question from an interview many years ago. Brother migrant workers summed it up and shared it with you. I hope it will help you, or maybe it will be used in the interview in the future.

First, let's take a look at the common Java Web servers.

  • Tomcat: a Web server provided by Apache organization, which provides support for jsp and Servlet. It is a lightweight java Web container (server) and the most widely used java Web server (free).
  • Jboss: is a Java EE compliant, open source, pure Java EJB server, which supports all Java EE specifications (free).
  • GlassFish: a java web server developed by Oracle company is a robust commercial server with product quality (few applications and charges).
  • Resin: it is the product of CAUCHO company, and it is a very popular application server. It provides good support for servlet s and JSP S, and its performance is also relatively good. Resin itself is developed in JAVA language (charging, more applications).
  • WebLogic: it is the product of Oracle company and the most widely used Web server at present. It supports the Java EE specification, and constantly improves to meet the new development requirements. It is suitable for large-scale projects (charging, using less, suitable for large companies).

Tomcat is the most common in the actual environment. Many times, especially in the normal test environment, multiple projects are tested at the same time. So today, brother migrant workers will talk with you about how to deploy multiple application projects under one Tomcat service.

1. Do not modify port

As you all know, the application project is placed directly under the Tomcat webapps directory

[root@CentOS7-1 tomcat]# cd webapps/
[root@CentOS7-1 webapps]# ll
total 4
drwxr-x--- 16 root root 4096 Jun  4 03:07 docs
drwxr-x---  6 root root   83 Jun  4 03:07 examples
drwxr-x---  5 root root   87 Jun  4 03:07 host-manager
drwxr-x---  5 root root  103 Jun  4 03:07 manager
drwxr-x---  3 root root  283 Jun  4 03:07 ROOT

Therefore, without modifying the port, we can directly add multiple project directories in this directory, or put the war package in this directory. Because of the test environment, we can directly simulate the directory after war decompression and replace it with the add directory.

[root@CentOS7-1 webapps]# mkdir test java
[root@CentOS7-1 webapps]# ls
docs examples host-manager java manager ROOT test

Prepare the first page file for testing

[root@CentOS7-1 webapps]# echo "this is a test" >test/test.html
[root@CentOS7-1 webapps]# echo "this is a java" >java/java.html
[root@CentOS7-1 webapps]# cat test/test.html
this is a test
[root@CentOS7-1 webapps]# cat java/java.html
this is a java

Modify profile

<!-- test -->    
<Context path="test/" docBase="test" reloadable="true" />  
<Context path="java/" docBase="java" reloadable="true" />
           #Add the above two lines of configuration
../conf/server.xml" 173L, 7744C

docBase property: Specifies the file path of the Web application, either absolute or relative

path property: Specifies the URL entry to access the Web application.

reloadable property: if this property is true, the tomcat server will monitor the changes of class files in the WEB-INF/classes and WEB-INF/lib directories under the running state. If it detects that the class files are updated, the server will automatically reload the Web application.

Restart the Tomcat service and test the access. The results are as follows:

Deployment succeeded.

Note: the added configuration steps in the configuration file can be skipped instead of necessary steps.

2. Modify port

The second method is to create multiple webapps directories in tomcat directory based on the improvement of the first method.

Delete the java project in the webapps directory and the test project in the webapps1 directory.

Modify profile

server.xml The configuration information of the first item already exists. Now you need to add the configuration of the second item. Under the Server node, add a Service node, and the second Service node can directly copy the first Service content for modification.

<Service name="Catalina1">  
 <!--The connectors can use a shared executor, you can define one or more named thread pools-->  
 <Connector port="8081" protocol="HTTP/1.1"  
               redirectPort="8443" />  
 <!-- A "Connector" using the shared thread pool-->  
 <Engine name="Catalina1" defaultHost="localhost">  
 <!-- Use the LockOutRealm to prevent attempts to guess user passwords  
           via a brute-force attack -->  
 <Realm className="org.apache.catalina.realm.LockOutRealm">  
 <!-- This Realm uses the UserDatabase configured in the global JNDI  
             resources under the key "UserDatabase". Any edits  
             that are performed against this UserDatabase are immediately  
             available for use by the Realm. -->  
 <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  
 <Host name="localhost"  appBase="webapps1"  
            unpackWARs="true" autoDeploy="true">  
 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log" suffix=".txt"  
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />  

Just pay attention to modifying a few configurations. For unfamiliar readers, you can use code tools to compare and find the differences between the two configurations, which will not be explained here.

  • Change the name attribute of the Service to Catelina1;
  • The port attribute of the connector accessed by http protocol is modified to 8081;
  • Change the name attribute of Engine to Catelina1;
  • The appBase property of the Host is modified to webapps1;

Restart service and test access

[root@CentOS7-1 conf]# ../bin/  
Using CATALINA_BASE: /usr/local/tomcat  
Using CATALINA_HOME: /usr/local/tomcat  
Using CATALINA_TMPDIR: /usr/local/tomcat/temp  
Using JRE_HOME: /usr  
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar  
Tomcat started.  

[root@CentOS7-1 conf]# lsof -i :8080  
java 2486 root 52u IPv6 25075 0t0 TCP *:webcache (LISTEN)  

[root@CentOS7-1 conf]# lsof -i :8081  
java 2486 root 57u IPv6 25079 0t0 TCP *:tproxy (LISTEN)  

[root@CentOS7-1 conf]# curl  
this is a test  

[root@CentOS7-1 conf]# curl  
this is a java

Tags: Java Tomcat Web Server Apache

Posted on Tue, 09 Jun 2020 00:17:28 -0400 by Negligence