Java 在 Tomcat 上部署 WAR 文件会给我一个 404 状态代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3365135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:36:11  来源:igfitidea点击:

Deploying a WAR file gives me a 404 Status Code on Tomcat?

javatomcatdeployment

提问by Legend

I am fairly new to Tomcat. I just managed to build a project and exported it as a WAR file. I tried manually copying a WAR file to the Tomcat folder then restarting. It created the folder structure and everything but I get a 404 Status code when I try to reach the application. I tried deploying it through the Tomcat Admin panel but I'm seeing the same behavior. Am I doing anything fundamentally wrong?

我对Tomcat相当陌生。我只是设法构建了一个项目并将其导出为 WAR 文件。我尝试手动将 WAR 文件复制到 Tomcat 文件夹,然后重新启动。它创建了文件夹结构和所有内容,但是当我尝试访问应用程序时,我得到了 404 状态代码。我尝试通过 Tomcat 管理面板部署它,但我看到了相同的行为。我做错了什么吗?

回答by hvgotcodes

Did you try

你试过了吗

localhost:8080/app

assuming your war file is app.war?

假设你的war文件是app.war?

edit for comment -- then your web.xml is screwed up.

编辑评论——那么你的 web.xml 就搞砸了。

回答by Robert Diana

My best guess is that you have not configured the welcome files in your web.xml. If you go to /app/ you need to have some default file available. Typically, people either created an index.html or they have welcome file settings to point the user to the correct jsp.

我最好的猜测是您还没有在 web.xml 中配置欢迎文件。如果你去 /app/ 你需要有一些可用的默认文件。通常,人们要么创建一个 index.html 要么他们有欢迎文件设置来将用户指向正确的 jsp。

回答by Thomas

Assuming that you have full privileges with the Tomcat installation, try going to http://localhost:8080/manager/html/, if you don't know the password to get into that area, look for the tomcat-users.xmlfile in your ${tomcat-installation}/confdirectory, and that should have it. If it has no entries in it then you will want to add an entry like the following to it:

假设您对 Tomcat 安装具有完全权限,请尝试转到http://localhost:8080/manager/html/,如果您不知道进入该区域的密码,请tomcat-users.xml在您的${tomcat-installation}/conf目录中查找该文件,该文件应该有。如果其中没有条目,则您需要向其中添加如下条目:

<user username="username" password="password" roles="admin,manager" />

See whether your application shows up in that screen, and if it does, click on the link and see if that gets you anywhere. If it doesn't, then I suggest following ifishers's advice, and looking at the log files to see if it lists any errors.

查看您的应用程序是否显示在该屏幕中,如果显示,请单击该链接并查看是否可以让您到达任何地方。如果没有,那么我建议遵循渔民的建议,并查看日志文件以查看它是否列出了任何错误。

But long story short, most likely something is screwed up with your project's web.xml

但长话短说,很可能是你的项目搞砸了 web.xml

回答by Harshil

I have the same problem. My problem is my application name and war file name are different. I have change war file name to my application name.

我也有同样的问题。我的问题是我的应用程序名称和 war 文件名不同。我已将 war 文件名更改为我的应用程序名称。

Here, when your application is deployed tomcat automatically extract your war file with same name. In my case my service name is my-web-service(i.e.https://localhost:8080/my-web-service/) and my war file name is MY-java-connector-0.0.1-SNAPSHOT.war(this is my project folder name). Now tomcat is looking for my-web=service folder and it finds epicor-java-connector-0.0.1-SNAPSHOT folder. so rename the folder to my-web=service.war and it is working.

在这里,当您的应用程序部署时,tomcat 会自动提取您的同名 war 文件。就我而言,我的服务名称是 my-web-service(即https://localhost:8080/my-web-service/),我的War文件名称是 MY-java-connector-0.0.1-SNAPSHOT.war(this是我的项目文件夹名称)。现在 tomcat 正在寻找 my-web=service 文件夹,它找到了 Epicor-java-connector-0.0.1-SNAPSHOT 文件夹。所以将文件夹重命名为 my-web=service.war 并且它正在工作。

回答by null

I had the same issue, my problem was that I was copying the file via sftp straight into the webapps dir of tomcat which may have caused a polling issue causing it to not deploy correctly. Tomcat monitors the webapps dir and when it sees the .xml it will crack on and do its thing, problem is that maybe everything hasn't been copied across yet. :(

我有同样的问题,我的问题是我通过 sftp 将文件直接复制到 tomcat 的 webapps 目录中,这可能导致轮询问题导致它无法正确部署。Tomcat 监控 webapps 目录,当它看到 .xml 时,它会破解并执行它的操作,问题是可能所有内容还没有被复制。:(

Best to create a staging folder, move or copy the war to that folder and then use linux mv command to move it into the webapps - mv is atomic so tomcat won't do anything until the operation is complete.

最好创建一个临时文件夹,将 war 移动或复制到该文件夹​​,然后使用 linux mv 命令将其移动到 webapps - mv 是原子的,因此在操作完成之前 tomcat 不会做任何事情。

Magic.

魔法。

Credit goes to a very very random post that I found on javarance which in turn lead to : http://pub.admc.com/howtos/tomcat/ch02.html#stagedir-section

归功于我在 javarance 上发现的一个非常非常随机的帖子,这反过来又导致:http://pub.admc.com/howtos/tomcat/ch02.html#stagedir-section

Hope this helps someone else :)

希望这对其他人有帮助:)

回答by Ohlaph

I had the same problem with tomcat and just fixed it by forcing my IDE to create a web.xml file. Mine was missing. It would deploy the project but I would still get the 404 error. I read through several of these tutorials but to no avail. I read somewhere that the web.xml file needed to be configured correctly. Went to find the web.xml in the WEB-INF folder, and there was no folder and no web.xml file. I did a quick google on how to create on using my IDE and then compiled my program with the web.xml output, and it fixed the problem for me. Hope that helps. Cheers!

我在使用 tomcat 时遇到了同样的问题,只是通过强制我的 IDE 创建一个 web.xml 文件来修复它。我的不见了。它会部署项目,但我仍然会收到 404 错误。我通读了其中的几个教程,但无济于事。我在某处读到 web.xml 文件需要正确配置。去WEB-INF文件夹里找web.xml,没有文件夹也没有web.xml文件。我对如何使用我的 IDE 创建一个快速的谷歌做了一个快速的谷歌,然后用 web.xml 输出编译了我的程序,它为我解决了这个问题。希望有帮助。干杯!

回答by Carrie Zhang

I ran into this problem, too.

我也遇到了这个问题。

My simple roundabout solution is to change docBase path.

我的简单迂回解决方案是更改 docBase 路径。

problem occurs when

问题发生时

docBase=".../tomcat/webapps/app_name.war";

problem solved with

问题解决了

docBase=".../tomcat/webapps/app_foldername/web"

回答by rogerdpack

FWIW I had this problem and fixed it by discovering I was accessing the wrong host, like "http://wronghost:8080/why_is_this_war_not_there" so if it says "XX.war successfully deployed" in catalina.out/localhost.*.out that may be a hint for followers.

FWIW 我遇到了这个问题并通过发现我访问了错误的主机来修复它,比如“ http://wronghost:8080/why_is_this_war_not_there”所以如果它在 catalina.out/localhost.*.out 中显示“XX.war 成功部署”这可能是对追随者的暗示。

回答by hello_earth

In more recent times, this condition might occur if JAVA_HOME points to an earlier version of Java than the code in the WAR. Tomcat might use JAVA_HOME to determine JRE_HOME it shows after running startup.bat in Tomcat's BIN directory. While Tomcat may be happy itself with the Java version it gets, for the application this might not be sufficient. In my case, Tomcat 8.0.49 was running alright with JDK1.7, while the application was not initializing at all (yes, it was a Spring Boot application, WAR was being unpacked, but that's all) - without giving any errors. After setting JAVA_HOME to the location of JDK1.8, the problem was solved.

最近,如果 JAVA_HOME 指向比 WAR 中的代码更早的 Java 版本,则可能会发生这种情况。Tomcat 可能会使用 JAVA_HOME 来确定它在 Tomcat 的 BIN 目录中运行 startup.bat 后显示的 JRE_HOME。虽然 Tomcat 可能对它获得的 Java 版本感到满意,但对于应用程序来说,这可能还不够。就我而言,Tomcat 8.0.49 在 JDK1.7 上运行良好,而应用程序根本没有初始化(是的,它是一个 Spring Boot 应用程序,WAR 正在被解包,但仅此而已)- 没有给出任何错误。将JAVA_HOME设置为JDK1.8的位置后,问题解决。

回答by geppy

If your'e using Spring-Boot like me to create your WAR file make sure to add the following to your main Application file.

如果您像我一样使用 Spring-Boot 创建 WAR 文件,请确保将以下内容添加到您的主应用程序文件中。

@SpringBootApplication
public class serverTestApplication extends SpringBootServletInitializer {


    public static void main(String[] args) {
        SpringApplication.run(serverTestApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(serverTestApplication.class);
    }
}

I was stuck on this for awhile, looked through dozens of guides. This is the video that finally showed me I never configured my application properly: https://www.youtube.com/watch?v=05EKZ9Xmfws

我被困在这个问题上一段时间,看了几十个指南。这是最终向我展示我从未正确配置我的应用程序的视频:https: //www.youtube.com/watch?v=05EKZ9Xmfws