HTTP 状态 404 - 在带有 Tomcat 的 Eclipse 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11290029/
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-09-10 21:09:02  来源:igfitidea点击:

HTTP Status 404 - on Eclipse with Tomcat

eclipseservletstomcat7

提问by user387184

I am trying just to run a servlet on my local Tomcat with Eclipse.

我正在尝试使用 Eclipse 在我的本地 Tomcat 上运行一个 servlet。

But I keep getting this error and do not have any idea what to do differently.

但是我不断收到此错误,并且不知道该怎么做。

I actually recorded it here : http://www.screenr.com/ZyD8

我实际上记录在这里:http: //www.screenr.com/ZyD8

Many thanks!

非常感谢!

Also I changed the web.xml to this:

我也将 web.xml 更改为:

      <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID"
    version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >

    <display-name>
TEST3
    </display-name>

    <welcome-file-list>

        <welcome-file>
TEST3
        </welcome-file>
    </welcome-file-list>

    <servlet>

        <servlet-name>
helloServlet
        </servlet-name>

        <servlet-class>
HelloServlet
        </servlet-class>
    </servlet>

    <servlet-mapping>

        <servlet-name>
helloServlet
        </servlet-name>

        <url-pattern>
/hello
        </url-pattern>
    </servlet-mapping>

</web-app>

回答by Hardik Mishra

I have seen your link.

我看过你的链接。

When ever you run any dynamic web project. By default Servletcontainer (which is Tomcat in this case) searches for files specified in wel-come list. Check your web.xml, it should contains entry like

当您运行任何动态 Web 项目时。默认情况下,Servlet容器(在本例中为 Tomcat)搜索欢迎列表中指定的文件。检查您的web.xml,它应该包含类似的条目

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

You haven't created file from any of the above list. So, running

您尚未从上述任何列表中创建文件。所以,运行

http://localhost:8080/TEST2will give you 404 error.

http://localhost:8080/TEST2会给你 404 错误。

Rather run : http://localhost:8080/TEST2/HelloSerlvetwill invoke the servlet which you have created.

而是运行 :http://localhost:8080/TEST2/HelloSerlvet将调用您创建的 servlet。

Edit: Check Project Menu of eclipse and verify "Build Automatically" is checked and Servlet container is running (http://localhost:8080).

编辑:检查 eclipse 的项目菜单并验证“自动构建”已选中并且 Servlet 容器正在运行 ( http://localhost:8080)。

Edit 2: Right Click Project --> Properties, Select Java Build Path --> source Tab --> Change Default output folder. Create /WEB-INF/classesunder /WebContent(default in eclipse)

编辑 2:右键单击项目 --> 属性,选择 Java 构建路径 --> 源选项卡 --> 更改默认输出文件夹。/WEB-INF/classes/WebContent(eclipse中默认)下创建

回答by Eric Q

This is based on the answer from Hardik Mishra with some highlights: 1. From the file explorer (not from Eclipse), Manually create the "/WEB-INF/classes" under /WebContent 2. Right Click Project --> Properties, Select Java Build Path --> source Tab --> Change Default output folder to the folder you just created above. 3. go to the file explorer, not from Eclipse, since the Eclipse "project Explorer" may have some filters that doesnot show the classes folder. You should see the .class files compiled under this directory

这是基于 Hardik Mishra 的答案,其中有一些亮点: 1. 从文件资源管理器(不是来自 Eclipse),在 /WebContent 下手动创建“/WEB-INF/classes” 2. 右键单击​​项目 --> 属性,选择Java Build Path --> source Tab --> 将默认输出文件夹更改为您刚刚在上面创建的文件夹。3. 转到文件资源管理器,而不是从 Eclipse,因为 Eclipse“项目资源管理器”可能有一些不显示类文件夹的过滤器。您应该会看到在此目录下编译的 .class 文件

Try to test it again. If it does not work, restart Eclipse for one time and then it should work.

尝试再次测试。如果它不起作用,请重新启动 Eclipse 一次,然后它应该可以工作。

回答by Bharath

I have been seeing these types of issue for quite sometime and have seen multiple solutions which work for some and rest still face the same issue.

我已经看到这些类型的问题有一段时间了,并且看到了多种解决方案,这些解决方案对某些人有效,而其他人仍然面临同样的问题。

One of the simple solution is traverse to the .java/.jsp/etc., right click and select run from server option.

一种简单的解决方案是遍历.java/.jsp/etc.,右键单击并选择从服务器运行选项。

I found this solution to be simple yet effective way of running.

我发现这个解决方案是一种简单而有效的跑步方式。

path Java Resource->src->->example.java-->right click-->run as-->run on server.

路径Java资源->src->->example.java-->右键单击-->运行方式-->在服务器上运行。

Even after this also you can face few issues like port 8005 not available, please follow the below link to clean out your current Apache setting and re-setting the same.

即使在此之后,您也可能会遇到一些问题,例如端口 8005 不可用,请按照以下链接清除您当前的 Apache 设置并重新设置相同的设置。

TOMCAT - HTTP Status 404

TOMCAT - HTTP 状态 404

Deployment error:Starting of Tomcat failed, the server port 8080 is already in use

部署错误:Tomcat启动失败,服务器8080端口已被占用

Hope this finding was helpful.

希望这个发现有帮助。

回答by Steven

Normally, when you modify the web.xml file, you should "clean" Tomcat. Just right-click on Tomcat in Eclipse and clean. Do same for project. You may also stop Tomcat, remove the app from Tomcat (right-click on app under Tomcat and remove) and then add it back. Restart Tomcat.

通常,当您修改 web.xml 文件时,您应该“清理”Tomcat。只需在 Eclipse 中右键单击 Tomcat 并清理。对项目做同样的事情。您也可以停止 Tomcat,从 Tomcat 中删除该应用程序(右键单击 Tomcat 下的应用程序并删除),然后将其添加回来。重启Tomcat。