简体   繁体   English

使用Java将网络应用上传到tomcat

[英]Upload web app to tomcat using Java

In my unit test I am using org.apache.catalina.startup.Tomcat to deploy a web app. 在单元测试中,我使用org.apache.catalina.startup.Tomcat部署Web应用程序。 I am able to deploy a webapp with .war format without any problem. 我能够部署.war格式的Web应用程序,而不会出现任何问题。 But in my case I want to add a value from java code to index.html of the webapp and then upload. 但就我而言,我想将Java代码中的值添加到webapp的index.html中,然后上传。 So I can't stick with .war format for this. 所以我不能坚持使用.war格式。 I tried to upload the web app as directory.Following is my java code to upload the "examplewebapp" as directory. 我试图将Web应用程序上传为目录。以下是我的Java代码,将“ examplewebapp”上传为目录。

    String appPath="/home/examplewebapp"; // webapp
    String currentDir = new File(".").getCanonicalPath();
    String tomcatDir = currentDir + File.separatorChar + "tomcat";


    Tomcat tomcat = new Tomcat();
    tomcat.setBaseDir(tomcatDir);
    tomcat.setPort(4040);
    tomcat.addWebapp("/examplewebapp", appPath);        
    tomcat.start();

but web app is not deployed on server as expected. 但是Web应用未按预期部署在服务器上。 I need to know whether tomcat.addWebapp() method supports only to .war format or there is any bug in my code. 我需要知道tomcat.addWebapp()方法仅支持.war格式,还是我的代码中有任何错误。

Step 1: Consult Lars Vogel's 4th chapter of his tutorial how to add Tomcat in Eclipse and follow his instructions. 步骤1:查阅Lars Vogel教程的第4章,了解如何在Eclipse中添加Tomcat并遵循他的指示。

Step 2: Click on your Web Project and go to step 3. 步骤2:单击您的Web项目,然后转到步骤3。

点击Web项目

Step 3: Click Run and let Eclipse start your Tomcat. 步骤3:单击Run(运行),然后让Eclipse启动Tomcat。

点击运行

Step 4: Select 'Run on Server' and continue with OK. 步骤4:选择“在服务器上运行”,然后单击确定。

在服务器上运行

Step 5: Choose existing Server and optionally enable the check box. 步骤5:选择现有服务器,并有选择地启用该复选框。

选择现有服务器

Step 6: Enjoy! 步骤6:尽情享受!

请享用

I think there is two errors in the above code 我认为上面的代码有两个错误

first yon didn't path to the correct folder ( String appPath="/home/examplewebapp"; // webapp) 第一步没有路径到正确的文件夹(字符串appPath =“ / home / examplewebapp”; // webapp)

second you need to call tomcat.getServer().await(); 第二,您需要调用tomcat.getServer()。await(); after tomcat.start(); 之后tomcat.start(); because the server will quit after it start. 因为服务器将在启动后退出。

see the below example 见下面的例子

String webappDirLocation = "src/main/webapp/"; 字符串webappDirLocation =“ src / main / webapp /”; Tomcat tomcat = new Tomcat(); Tomcat tomcat =新的Tomcat();

    //The port that we should run on can be set into an environment variable
    //Look for that variable and default to 8080 if it isn't there.
    String webPort = System.getenv("PORT");
    if(webPort == null || webPort.isEmpty()) {
        webPort = "8080";
    }

    tomcat.setPort(Integer.valueOf(webPort));

    tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
    System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

    tomcat.start();
    tomcat.getServer().await();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM