繁体   English   中英

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

[英]Upload web app to tomcat using Java

在单元测试中,我使用org.apache.catalina.startup.Tomcat部署Web应用程序。 我能够部署.war格式的Web应用程序,而不会出现任何问题。 但就我而言,我想将Java代码中的值添加到webapp的index.html中,然后上传。 所以我不能坚持使用.war格式。 我试图将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();

但是Web应用未按预期部署在服务器上。 我需要知道tomcat.addWebapp()方法仅支持.war格式,还是我的代码中有任何错误。

步骤1:查阅Lars Vogel教程的第4章,了解如何在Eclipse中添加Tomcat并遵循他的指示。

步骤2:单击您的Web项目,然后转到步骤3。

点击Web项目

步骤3:单击Run(运行),然后让Eclipse启动Tomcat。

点击运行

步骤4:选择“在服务器上运行”,然后单击确定。

在服务器上运行

步骤5:选择现有服务器,并有选择地启用该复选框。

选择现有服务器

步骤6:尽情享受!

请享用

我认为上面的代码有两个错误

第一步没有路径到正确的文件夹(字符串appPath =“ / home / examplewebapp”; // webapp)

第二,您需要调用tomcat.getServer()。await(); 之后tomcat.start(); 因为服务器将在启动后退出。

见下面的例子

字符串webappDirLocation =“ src / main / webapp /”; 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