当我运行以下代码时,我收到一个 Servlet 404 错误 [重复]

     2023-03-25     91

关键词:

【中文标题】当我运行以下代码时,我收到一个 Servlet 404 错误 [重复]【英文标题】:When I am running the following code, I am receiving an Servlet 404 error [duplicate] 【发布时间】: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/Project/WEB-INF/classes/yi/Main.java 尝试点击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 【参考方案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”

【讨论】:

当我尝试运行此代码时,出现以下错误

】当我尝试运行此代码时,出现以下错误【英文标题】:Wheniamtryingtorunthiscodeiamgettingthefollowingerror【发布时间】:2019-01-2619:53:28【问题描述】:我正在尝试通过此代码连接mongodb数据库,但在运行此代码时出现错误(代码底部的错... 查看详情

我收到一个 AttributeError

...nAttributeError【发布时间】:2021-04-2311:04:14【问题描述】:当我运行以下代码时,出现上述错误,在下面的代码中,我正在尝试制作集群。请帮我纠正这个错误这个错误。importpandasaspdimportnumpyasnpfrommatplotlibimportpyplotasplt%matplotlibinlin... 查看详情

从 servlet 运行 C++ exe

】从servlet运行C++exe【英文标题】:runningC++exefromservlet【发布时间】:2012-07-0308:21:29【问题描述】:我正在我的机器上运行一个服务器。当Servlet收到消息后,相应的VisualC++".exe"需要开始运行。我正在使用以下代码来启动exe。但我... 查看详情

当我尝试在单击按钮时运行 AsyncTask 时,我收到一个致命错误“在 MainActivity 中找不到方法 createView(View)”

】当我尝试在单击按钮时运行AsyncTask时,我收到一个致命错误“在MainActivity中找不到方法createView(View)”【英文标题】:WhenItrytorunanAsyncTaskonabuttonclick,Ireceiveafatalerror"CouldnotfindamethodcreateView(View)inMainActivity"【发布时间】:201... 查看详情

当我没有收到手动运行代码时,在 Python for 循环中收到 KeyError

】当我没有收到手动运行代码时,在Pythonfor循环中收到KeyError【英文标题】:ReceivingaKeyErrorinaPythonforloopwhenIdonotreceiveonerunningthecodemanually【发布时间】:2020-11-1223:54:37【问题描述】:我正在尝试使用geopy库来获取旧金山10个社区的... 查看详情

当我运行herokups:scaleweb=1命令我收到此错误。谁能帮我这个(代码片段)

当我运行herokups:scaleweb=1命令时,我收到此错误。▸Missingrequiredflag:▸-a,--appAPPapptoruncommandagainst▸Seemorehelpwith--help谁能帮我这个。答案您必须提及您打算从CLI运行命令的应用程序名称,尽管只有一个应用程序。从CLI运行命令时,建... 查看详情

当我使用 YOLACT 运行 train.py 时,我收到错误 KeyError: 0

】当我使用YOLACT运行train.py时,我收到错误KeyError:0【英文标题】:WhenIruntrain.pywithYOLACT,IgettheerrorKeyError:0【发布时间】:2021-02-0119:11:49【问题描述】:我是机器学习和编程的新手。现在我正在尝试使用我自己的数据开发YOLACTAI。但... 查看详情

我收到“运行时 API 错误:设备序号无效。”当我使用 GTX 590 在 Ubuntu 10.04 上运行 cuda 代码时

】我收到“运行时API错误:设备序号无效。”当我使用GTX590在Ubuntu10.04上运行cuda代码时【英文标题】:Iget"RuntimeAPIerror:invaliddeviceordinal."whenIruncudacodeonUbuntu10.04usingaGTX590【发布时间】:2012-10-1914:00:49【问题描述】:我正在... 查看详情

当我尝试在我的终端上运行代码时,我不断收到“ModuleNotFound”错误,即使我安装了它

】当我尝试在我的终端上运行代码时,我不断收到“ModuleNotFound”错误,即使我安装了它【英文标题】:Ikeepgetting"ModuleNotFound"errorwhenitrytoruncodeonmyterminaleventhoughiinstalledit【发布时间】:2021-12-0715:46:19【问题描述】:我与:... 查看详情

当我尝试运行 Xcode 模拟器时,我收到错误“停止“(null)”?

】当我尝试运行Xcode模拟器时,我收到错误“停止“(null)”?【英文标题】:WhenItrytoruntheXcodesimulatorIgettheerror\'Stop“(null)”?\'【发布时间】:2015-12-1811:16:06【问题描述】:我刚刚更新到Xcode7并在实时设备上运行了一个应用程序... 查看详情

当我尝试在形成字符串时运行以下代码时,Python 抛出 valueError

】当我尝试在形成字符串时运行以下代码时,Python抛出valueError【英文标题】:PythonthrowsvalueErrorwhenItrytorunthefollowingcodewhileformingstring【发布时间】:2018-05-1318:05:44【问题描述】:importdatetimefromdatetimeimportdateStart_Date=date(2010,01,01)marke... 查看详情

运行 servlet 显示错误

】运行servlet显示错误【英文标题】:Runningservletshowserror【发布时间】:2017-08-2100:44:18【问题描述】:我在eclipse中的java文件中写了以下代码,看到了一个教程。当我尝试在服务器上运行它时,它会显示给我Http:404errorTypeStatusReportD... 查看详情

运行码头时出现错误 NoClassDefFoundError

...码头服务器我有maven3.6.0和OpenJdk11.0.4我的第一次运行失败当我启动我的代码时收到以下错误:线程“main”中的异常java.lang.NoClassDefFoundErro 查看详情

玩! 1.2.4 - 服务器启动时未收到数据

...。我可以在我的计算机上运行另一个播放应用程序,但是当我尝试运行它时,它会启动服务器,但似乎没有在第一次请求时初始化。我启用了TRACE日志记录,结果如下:11:21:29,894INFO~ListeningforH 查看详情

我已经使用 IntelliJ 创建了一个 jar,但是当我尝试运行该 jar 时,我收到一个错误,因为无法加载主类清单 [重复]

】我已经使用IntelliJ创建了一个jar,但是当我尝试运行该jar时,我收到一个错误,因为无法加载主类清单[重复]【英文标题】:IhavecreatedajarusingIntelliJbutwhenItrytorunthejarthenIamgettinganErrorasFailedtoloadMainclassmanifest[duplicate]【发布时间】... 查看详情

当我运行“npm install”时,我遇到了我不明白的 npm 问题

】当我运行“npminstall”时,我遇到了我不明白的npm问题【英文标题】:IamhavingnpmissuesIdonotunderstandwhenIrun`npminstall`【发布时间】:2021-08-0204:08:46【问题描述】:当我运行npminstall时,我收到以下错误,我不知道如何解决它。我尝试... 查看详情

当我创建同义词时,我收到以下错误

】当我创建同义词时,我收到以下错误【英文标题】:wheniamcreatingsynonymiamgettingfollowingerror【发布时间】:2017-12-0309:32:47【问题描述】:输入用户名:scott/tiger连接到:OracleDatabase11g企业版11.2.0.1.0-64位生产具有分区、OLAP、数据挖... 查看详情

你需要一个基于 shiboken 的类型

...boken-basedtype【发布时间】:2015-06-2913:47:32【问题描述】:当我运行代码时,我收到以下错误第11行:您需要基于shiboken的类型。不知道我在这里做错了什么。当我只运行GetMayaWindow()时,它运行正常,但是在init中运行时,它给了我... 查看详情