【问题标题】:HTTP Status 404 - Why server can't find my servlet I mapped to?HTTP 状态 404 - 为什么服务器找不到我映射到的 servlet?
【发布时间】:2021-03-22 12:25:17
【问题描述】:

首先,对不起,如果这个问题有点长。我保证代码超级简单且易于阅读。

我使用 Servlets 制作了一个非常简单的应用程序。它有 form,其中 user 选择啤酒颜色。它应该将他带到我使用 XML 映射到的 servletServlet 应该只发回一个响应 并打印用户选择的颜色。

不幸的是,我在某个地方有一个错误,我不知道在哪里。有人可以帮助我吗,因为我真的被困住了?

有我的三个文件:

form.html

<!DOCTYPE html>
<html>
<body>

    <form action="SelectBeer.do" method="POST">

        Color:
        <select name="color">
            <option>light</option>
            <option>amber</option>
            <option>brown</option>
            <option>dark</option>
        </select>
        
        <button>Submit</button>

    </form>

</body>
</html>

BeerSelect.java servlet:

package com.example.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BeerSelect extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        String color = req.getParameter("color");
        out.println("Selected: " + color);

    }
}

web.xml,我用于映射的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" id="WebApp_ID" version="3.0">
  <display-name>MyBeerApp</display-name>
  <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>
  
  <servlet>
  <servlet-name>myServlet</servlet-name>
  <servlet-class>com.example.web.BeerSelect</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>myServlet</servlet-name>
  <url-pattern>/SelectBeer.do</url-pattern>
  </servlet-mapping>
  
</web-app>

最后,我将从我的项目结构中为您提供 ss。再次,提前感谢任何试图提供帮助的人。这对我来说意义重大!

附:表单运行良好,但提交时出现 404 错误。

【问题讨论】:

  • 您是否尝试打开浏览器开发人员工具来检查单击提交按钮时的 URL 是什么?这可能有助于检查它是否访问了正确的 URL
  • 它是http://localhost:8080/MyBeerApp/SelectBeer.do。在 XML 中,我使用的是/SelectBeer.do 的 url-pattern,应该没问题吧?
  • 尝试重新编译项目

标签: java xml servlets server


【解决方案1】:

我认为您的代码没有错。我已经按照您的预期尝试运行和结果。

【讨论】:

  • 我不敢相信。它怎么能对你有用而不对我有用?我累了好几次,仍然没有运气。我每次都收到错误 404。你们也有同样的项目结构吗?
  • 是的。这与您的项目相同。你能把你的项目发给我吗?
  • 我只是重新写了一遍。我注意到一个不同之处。我需要将 HTML 放在 WebContent 中,将 XML 放在 WEB-INF 中。我的 4 个小时到了……
  • 您能帮我检查一下您的项目结构吗?我发布的问题图片是错误的,对吧?我的意思是,如果你像我以前那样做,它不可能工作?
  • 再次感谢,您现在可以删除此答案 :) 我接受了您的第二个
【解决方案2】:

@Stefan 这是我的项目结构。我发现它和你有点不同。

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 2015-07-19
    • 2019-07-30
    • 2016-03-25
    • 2020-04-04
    • 2019-07-30
    • 2020-07-31
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多