错误 404 - 请求的资源在带有 tomcat 7 的 Eclipse 中不可用

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

Error 404 - The requested resource is not available in Eclipse with tomcat 7

eclipsetomcatweb-applicationseclipse-wtp

提问by Humunculus84

I am trying to run a simple web-app with eclipse and tomcat 7. I an index html file with a text input and a submit button that calls on servlet.

我正在尝试使用 eclipse 和 tomcat 7 运行一个简单的 web 应用程序。我是一个带有文本输入的索引 html 文件和一个调用 servlet 的提交按钮。

I have created a Dynamic web project and have put the html (index.html) file in the WebContent folder.

我创建了一个动态 Web 项目并将 html (index.html) 文件放在 WebContent 文件夹中。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>title</title>
</head>
<body>
    <form action="/TestServ" method="get">
        <input type="text" name="input">
        <input type="submit" value="Send it!">
    </form>
</body>
</html>

I have my package com.testing.web under /Java Rescources/src and in it my servlet (TestServ.java) which looks loke this:

我的包 com.testing.web 位于 /Java Resources/src 下,其中包含我的 servlet (TestServ.java),它看起来像这样:

@WebServlet("/TestServ")
public class TestServ extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServ() {
        super();
        // TODO Auto-generated constructor stub
        System.out.println("AA%WU^SDJI&^R&KU");
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("AA%WU^SDJI&^R&KU");
    }
}

When i select Run->run on server on the project the index.html file loads just fine. The problem is that when i submit the form i get the following error page:

当我在项目上选择 Run->run on server 时,index.html 文件加载得很好。问题是,当我提交表单时,出现以下错误页面:

HTTP Status 404 - /TestServ

--------------------------------------------------------------------------------

type Status report

message /TestServ

description The requested resource is not available.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.34

I have searched and searched and haven't found a solution that helps me. Most answers around the web point to servlet url mapping mistakes. Note that i have tried many of them to no avail.

我已经搜索并搜索过,但没有找到对我有帮助的解决方案。网络上的大多数答案都指向 servlet url 映射错误。请注意,我已经尝试了很多都无济于事。

Furthermore i would like to add that my server is configured to use jdk1.7.0_10 in the runtime enviroments oprtion.

此外,我想补充一点,我的服务器配置为在运行时环境选项中使用 jdk1.7.0_10。

Finally, i do not understand why eclipse doesn't create the proper class/ structure under WEB-INF when the project is built.

最后,我不明白为什么 eclipse 在构建项目时没有在 WEB-INF 下创建正确的类/结构。

I am totally buffled and any help will be much appreciated. Thank you very much in advance!

我完全不知所措,任何帮助将不胜感激。非常感谢您提前!

回答by Humunculus84

Finally found the problem, and as usual it was the simplest thing.

终于找到问题了,和往常一样是最简单的事情了。

The action in the form needed to be like this:

表单中的操作需要是这样的:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>title</title>
</head>
<body>
    <form action="/TestProj/TestServ" method="get">
        <input type="text" name="input">
        <input type="submit" value="Send it!">
    </form>
</body>
</html>

and the url mapping as follows:

和 url 映射如下:

<servlet>
    <servlet-name>TestServ</servlet-name>
    <servlet-class>com.testing.web.TestServ</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServ</servlet-name>
    <url-pattern>/TestServ</url-pattern>
  </servlet-mapping>

frankly i don't perfectly understand why though. Shouldn't /Testproj be considered the root of the app anyway? :/ Anyway...

坦率地说,我并不完全理解为什么。无论如何不应该将 /Testproj 视为应用程序的根目录吗?:/ 反正...

回答by user3081796

in order to remove 404 error we have to check the web.xml and the url you are giving in the tab. url should be like http://<127.0.0.1:8080>//. if it is webservice application

为了删除 404 错误,我们必须检查 web.xml 和您在选项卡中提供的 url。url 应该像 http://<127.0.0.1:8080>//。如果是网络服务应用程序

回答by Chamath Dilanka

Nothing wrong with your server configuration. Need some modifications in web.xml file. Just try this!

您的服务器配置没有问题。需要在 web.xml 文件中进行一些修改。试试这个!

  1. Open your "web.xml" file.
  2. Add these tags between "<display-name>" tag & "<welcome-file-list>" tag.

    <servlet> <servlet-name>TestServ</servlet-name> <servlet-class>TestServ</servlet-class> </servlet>

    <servlet-mapping> <servlet-name>TestServ</servlet-name> <url-pattern>/TestServ</url-pattern> </servlet-mapping>

  3. Run your project and in browser enter URL as "http://localhost:8080/TestServ"

  1. 打开您的“web.xml”文件。
  2. 在“ <display-name>”标签和“ <welcome-file-list>”标签之间添加这些标签。

    <servlet> <servlet-name>TestServ</servlet-name> <servlet-class>TestServ</servlet-class> </servlet>

    <servlet-mapping> <servlet-name>TestServ</servlet-name> <url-pattern>/TestServ</url-pattern> </servlet-mapping>

  3. 运行你的项目并在浏览器中输入 URL 作为“ http://localhost:8080/TestServ

Hope it will work! :)

希望它会起作用!:)

P.S. * You can actually use any name between "<servlet-name>" tags. But names in both places must be same.

PS *您实际上可以在“ <servlet-name>”标签之间使用任何名称。但是两个地方的名字必须相同。