由 java.lang.NoSuchMethodException ronit.hello 导致实例化 servlet 类时出现状态 500 错误<init>

问题描述 投票:0回答:1

我正在执行一个 Web 项目,它抛出错误 500 错误实例化 servlet 类。 我正在使用 tomcat 9.0.71 并使用 web 模块 4.0 .

这是屏幕上的错误消息。

This is the error page status 500

这是 HTML 文档

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>This is a hello world</h1>
<br>
<form action ="hello" method="get">
<input type="submit">click to say hello world
</form>
</body>
</html>

这是 servlet 类

package ronit;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;
@WebServlet("/hello")
class hello extends HttpServlet{
 static final long serialVersionUID = 35335L;
public void doGet(HttpServletRequest req, HttpServletResponse res) {

try {
PrintWriter p = res.getWriter();

p.println("Hello World");
}catch(IOException e) {e.printStackTrace();}
}}


这是服务器中的部署配置

This is the servlet view

This is the console after execution

This is the project explorer view

我使用注释而不是 web.xml,并通过右键单击项目并在服务器上运行来部署文件。

java servlets http-status-code-500 nosuchmethod
1个回答
0
投票

您的 servlet 类没有公共的、零参数的构造函数。它需要一个,否则 Tomcat 无法实例化它——正如错误输出所告诉您的那样。

© www.soinside.com 2019 - 2024. All rights reserved.