【问题标题】:When I am running the following code, I am receiving an Servlet 404 error [duplicate]当我运行以下代码时,我收到一个 Servlet 404 错误 [重复]
【发布时间】:2021-07-26 11:31:18
【问题描述】:

我正在运行以下代码,它是 html 版本,它显示了姓名和年龄框。

<!-- Main.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A First page</title>
</head>
<body>
  <form action="Thanks.jsp" method="get">
    Enter Your Name: <input type="text" name="yourName"><br>
    Enter Your Age&nbsp;&nbsp;&nbsp;&nbsp;:<input type="text" name="yourAge"><br>
    <input type="submit" value="Sumitting">
  </form>
</body>
</html>

但是当我运行相同逻辑的 servlet 版本的代码时,它得到了一个错误

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. 

代码如下:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/Main")
public class Main extends HttpServlet {
  private static final long serialVersionUID = 1L;
  public Main() {
    super();
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse   
    response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();     //Is added
    out.println("<form action=\"http://localhost\" method=\"post\">");
    out.println("Enter Your Name: <input type=\"text\" name " +
      "= \"yourName=\" </input><br>");
    out.println("Enter Your Age&nbsp;&nbsp; : <input " + 
      "type=\"text\" name = \"yourAge=\" </input>");
    out.close();
  }
}

根据@RomanC 的要求添加 web.xml

<?xml version="1.0" encoding="UTF-8"?>
   <web-app xmlns:xsi="w3.org/2001/XMLSchema-instance" 
    xmlns="xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee 
    xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> 
    
    <servlet> 
        <servlet-name>abc</servlet-name> 
        <servlet-class>yi.Main</servlet-class> 
    </servlet> 
</web-app>

【问题讨论】:

  • 您为获得响应而点击的 URL 是什么?
  • 尝试点击localhost:8080/main 我建议您了解 servlet 的工作原理。您缺少的一点是 servlet 的基础。您使用 URL 的方式不正确。此外,您正在尝试从类 WEB-INF/classes 中访问 Main.java,这表明您还需要学习 Java 基础知识。基本上,一旦 Java 类被编译,就会创建一个包含字节码的 .class 文件。在您的 WAR 文件中,它将是 WEB-INF/classes/main.class 但您不能直接访问它。您将需要访问您为 servlet 指定的 URL。
  • 你能给我看看你的 web.xml 吗?
  • w3.org/2001/XMLSchema-instance" xmlns="xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation=" xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> abcyi.Main

标签: servlets http-status-code-404


【解决方案1】:

确保在 servlet 中添加所有 html 元素(标签)。

试试这个:

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();     //Is added

        String htmlTag
                = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";

        out.println(htmlTag
                + "<html>\n"
                + "<head><title>Reg Form</title></head>\n"
                + "<body bgcolor = \"#f0f0f0\">\n"
                + "<h1 align = \"center\">Regform</h1>\n"
                + "<ul>\n"
                + "<form action=\"http://localhost\" method=\"post\">"
                + "Enter Your Name: <input type=\"text\" name = \"yourName\"</input><br><br>\n"
                + "Enter Your Age&nbsp;&nbsp; : <input type=\"text\" name = \"yourAge\" </input>\n"
                + "</ul>\n"
                + "</body>"
                + "</html>");

        out.close();
}

当您使用@Webservlet Annotation 时,您不需要在 web.xml 中提及 servlet 映射,但如果您正在寻找 xml,仍然需要提及

  <servlet>
        <servlet-name>Main</servlet-name>
        <servlet-class>test.Main</servlet-class>  //test is the Package
    </servlet>
    <servlet-mapping>
        <servlet-name>Main</servlet-name>
        <url-pattern>/Main</url-pattern>
    </servlet-mapping>

运行 Servlet 文件,您将获得准确的 URL。(右键单击 Servlet 并运行文件)

一般是这样的:“http://localhost:8084/NameofYourApplication/Main”

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 2021-08-26
    • 2017-02-23
    • 1970-01-01
    • 2020-06-23
    • 2017-10-04
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多