Several ways to deploy Java Web applications in Tomcat

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. 
1. Static deployment 
     Static deployment means that we deploy our program before the server starts, and our Web application can only be accessed after the server starts. The following three methods can be deployed: 
    1. Copy the PetWeb directory to $CATALINA_HOME\webapps, and then start the server. This way is simpler, but the web application must be in the webapps directory. The access address is as follows: 
http://localhost:8080/PetWeb/    2. In this way, you can deploy directly in F:\ without copying the PetWeb directory to webapps. The method is as follows, change the $CATALINA_HOME\conf\server.xml file, add the <Context> tag in the <host> tag, the content is as follows:  <Context docBase="F:/PetWeb" reloadable="false" path="/Pet" />  Where reloadable="false" means that the server will not automatically load when the content in the application is changed. This attribute is usually set to true in the development phase, which is convenient for development. It should be set to false in the release phase to improve the application's performance. access speed. docBase is a path, you can use an absolute path, you can also use a relative path, the relative path is relative to webapps. The value of the path attribute is the root address when accessing. The access address is as follows: http://localhost:8080/Pet/     3. This method is similar to the second method, but not in Server.  


 

<Context docBase="F:/PetWeb" reloadable="false" /> You may find that it is similar to the second method, but the path attribute is missing. In this way, the server will use the .xml name as the value of the path attribute. The access address is as follows: http://localhost:8080/Pet/     We just deployed the PetWeb folder on the server. We know that the content of the Web application can be packaged into a .war package and then deployed on the server. Please refer to the following steps for packaging:      1. Open the command prompt (Start-->Run-->cmd)      2. Set the jdk environment variable      3. After entering the F:\PetWeb file in the command prompt, type the following command:  jar . .\Pet.war *.* This should have Pet.war file under F:\. where .. represents the parent directory of the current directory.      Deploying the Pet.war file is very simple, just change the docBase="F:\PetWeb" to docBase="F:\Pet.war" or copy it directly to the webapps. Restart the server to deploy Pet.war as a web application. If you are careful enough, you will find that the server unpacks the Pet.war file, and generates a Pet folder under webapps, and then copies the contents of Pet.war into it. We can cancel automatic decompression in the following ways, the configuration is as follows:  <Context docBase="F:/PetWeb" reloadable="false" unpackWAR="false"/>  Second, dynamic deployment  
    








    Dynamic deployment means that a web application can be deployed after the server is started without restarting the server. The manager.war file provided by the server is used for dynamic deployment. If the file does not exist under $CATALINA_HOME\webapps\, you must download tomcat again, otherwise the following functions cannot be completed. To use this management program, you must first edit the $CATALINA_HOME\conf\tomcat-users.xml file, 
and the tomcat-users.xml file mentioned above, the content of this file is not dependent on any web application, so in any It can be used in a web program. If these users and roles only work for your application, then you can place them in your own application, such as WEB-INF. Next, we will add tomcat-users last time The content in the .xml file is added to the myUser.xml file we created under WEB-INF. Note that this file should be the root directory of <tomcat-users>, as shown below: 
<tomcat-users> 
  <!- -Define roles--> 
  <role rolename="suozhangshi"/> 
  <role rolename="guahaoshi"/> 
  <role rolename="danganshi"/> 
  <!--Define users and add them to roles--> 
  <user username="gua1" password="gua1" roles="guahaoshi"/> 
  <user username="gua2" 
  <user username="dang1" password="dang1" roles="danganshi"/> 
  <user username="dang2" password="dang2" roles="danganshi"/> 
  <user username="suo1" password="suo1" roles="suozhangshi"/> 
  <user username="suo2" password="suo2" roles="suozhangshi"/> 
</tomcat-users> 

    这个文件已经建立好了,那么怎么告诉Tomcat加载这个文件呢?我们通过下面这一种方式,在你的Web应用程序中的META-INF文件夹中加入Context.xml 文件,这样当此应用程序部署的时候,就会加载该文件的内容,内容如下: 
<Context> 
  <Realm className="org.apache.catalina.realm.MemoryRealm" 
         pathname="webapps/SecurityWeb/WEB-INF/myUsers.xml" /> 
</Context> 
    Realm标签中className属性定义了使用MemoryRealm类从pathname处加载xml文件。。还要注意这里的pathname它是以相对Tomcat根目录的,所以上文件路径应该从webapps开始。最后重新启动服务器再访问你程序,应该能够看到和上次相同的效果

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692158&siteId=291194637