java - 如何修复错误 404 : The origin server did not find a current representation for the target resource

标签 java jsp servlets http-status-code-404

所以我对 servlet 和 jsp 很陌生,并且正在遵循 https://stackoverflow.com/tags/servlets/info 中的 Hello World 后处理示例。 。当我尝试在 Eclipse 中使用 Tomcat v9.0 运行它时,我得到

enter image description here

在做了一些额外的研究和探索之后,我还没有找到可行的解决方案或对正在发生的事情的解释。我基本上是从示例中复制了代码,所以我很困惑为什么它不起作用。我还没有真正熟练地弄清楚到底出了什么问题,所以任何帮助都会很棒。到目前为止,我唯一的预感是我可能搞乱了目录或其他东西。这是一张图片:

enter image description here

我能发现的唯一差异是我的 HelloServlet.class 所在的位置,它位于

 apache-tomcat-9.0.19/webapps/hello/build/classes/com/example/controller/HelloServlet.class

而不是

/WEB-INF/classes/com/example/controller/HelloServlet.class

如示例中所述。我认为这是因为 Eclipse 默认情况下已经编译了类文件,但为了确定,我将类文件夹复制到 WEB-INF 中,以便它与示例匹配,但它仍然不起作用。这就是我被困住的地方。如果有人能指出我的错误,甚至提供帮助,我将不胜感激。我在下面包含了 hello.jsp、web.xml 和 HelloServlet.java 文件,以防万一它们出现任何问题。

hello.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Servlet Hello World</title>
        <style>.error { color: red; } .success { color: green; }</style>
    </head>
    <body>
        <form action="hello" method="post">
            <h1>Hello</h1>
            <p>
                <label for="name">What's your name?</label>
                <input id="name" name="name" value="${fn:escapeXml(param.name)}">
                <span class="error">${messages.name}</span>
            </p>
            <p>
                <label for="age">What's your age?</label>
                <input id="age" name="age" value="${fn:escapeXml(param.age)}">
                <span class="error">${messages.age}</span>
            </p>
            <p>
                <input type="submit">
                <span class="success">${messages.success}</span>
            </p>
        </form>
    </body>
</html>

web.xml

<web-app 
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">
</web-app>

HelloServlet.java

package com.example.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

@SuppressWarnings("serial")
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Preprocess request: we actually don't need to do any business stuff, so just display JSP.
        request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Postprocess request: gather and validate submitted data and display the result in the same JSP.

        // Prepare messages.
        Map<String, String> messages = new HashMap<String, String>();
        request.setAttribute("messages", messages);

        // Get and validate name.
        String name = request.getParameter("name");
        if (name == null || name.trim().isEmpty()) {
            messages.put("name", "Please enter name");
        } else if (!name.matches("\\p{Alnum}+")) {
            messages.put("name", "Please enter alphanumeric characters only");
        }

        // Get and validate age.
        String age = request.getParameter("age");
        if (age == null || age.trim().isEmpty()) {
            messages.put("age", "Please enter age");
        } else if (!age.matches("\\d+")) {
            messages.put("age", "Please enter digits only");
        }

        // No validation errors? Do the business job!
        if (messages.isEmpty()) {
            messages.put("success", String.format("Hello, your name is %s and your age is %s!", name, age));
        }

        request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
    }

}

最佳答案

您需要允许跨源

private void setAccessControlHeaders(HttpServletResponse resp) { resp.setHeader("Access-Control-Allow-Origin", "http://localhost:9000"); resp.setHeader("Access-Control-Allow-Methods", "GET"); }

关于java - 如何修复错误 404 : The origin server did not find a current representation for the target resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089493/

相关文章:

java - 为什么这个 .split 会导致数组越界错误?

java - 停止 Java 时不执行存储过程

java - Java中使用scanner方法进行简单计算

javascript - 下拉值未填充

java - Spring MVC HTTP 状态 400 - 错误请求

java - 尝试以一定的时间间隔在网页上打印,但它一直在等待并立即打印

java - 用于敏感用户数据的 SSL Servlet,用于其他所有内容的 NonSSL Servlet

javascript - 服务器端Cookies的访问

Java,传递实现接口(interface)的对象

java - 批量更新从更新中返回了意外的行数(Spring/Hibernate)