首页 >Java >如何修复 java.lang.NoSuchMethodException: Servlets.MyServlet.() at java.base/java.lang.Class.getConstructor0

如何修复 java.lang.NoSuchMethodException: Servlets.MyServlet.() at java.base/java.lang.Class.getConstructor0

WBOY
WBOY转载
2024-02-09 11:33:18525浏览

php小编子墨为您解答修复"java.lang.NoSuchMethodException: Servlets.MyServlet.() at java.base/java.lang.Class.getConstructor0"的方法。这个错误通常发生在Java程序中,表示找不到指定的构造方法。要解决这个问题,可以检查代码中的构造方法是否存在、参数是否正确,还可以检查类路径是否正确配置。另外,还可以考虑升级Java版本或者使用其他可用的构造方法。通过这些方法,您应该能够成功修复这个错误并使程序正常运行。

问题内容

我在 jsp 页面中有一个表单,我正在尝试连接到 servlet myservlet。我使用的是 tomcat 10.x。我不断收到以下错误:

http status 500 – internal server error

type exception report

message error instantiating servlet class [servlets.myservlet]

description the server encountered an unexpected condition that prevented it from fulfilling the request.

exception

jakarta.servlet.servletexception: error instantiating servlet class [servlets.myservlet]
    org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:482)
    org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:93)
    org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:673)
    org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:340)
    org.apache.coyote.http11.http11processor.service(http11processor.java:391)
    org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:63)
    org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:896)
    org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1744)
    org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:52)
    org.apache.tomcat.util.threads.threadpoolexecutor.runworker(threadpoolexecutor.java:1191)
    org.apache.tomcat.util.threads.threadpoolexecutor$worker.run(threadpoolexecutor.java:659)
    org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)
    java.base/java.lang.thread.run(thread.java:1589)
root cause

java.lang.nosuchmethodexception: servlets.myservlet.<init>()
    java.base/java.lang.class.getconstructor0(class.java:3641)
    java.base/java.lang.class.getconstructor(class.java:2324)
    org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:482)
    org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:93)
    org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:673)
    org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:340)
    org.apache.coyote.http11.http11processor.service(http11processor.java:391)
    org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:63)
    org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:896)
    org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1744)
    org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:52)
    org.apache.tomcat.util.threads.threadpoolexecutor.runworker(threadpoolexecutor.java:1191)
    org.apache.tomcat.util.threads.threadpoolexecutor$worker.run(threadpoolexecutor.java:659)
    org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)
    java.base/java.lang.thread.run(thread.java:1589)
note the full stack trace of the root cause is available in the server logs.

apache tomcat/10.1.18

我知道它没有在给定位置找到 servlet(?),但我不确定如何解决该问题。任何帮助将不胜感激。

这是我的文件:

index.jsp

<html>
<body>
<h2>hello world!</h2>
<form action="myservlet" method="get">
    <input type="submit" value="enter">
</form>
</body>
</html>

myservlet.java

package servlets;

import jakarta.servlet.servletexception;
import jakarta.servlet.annotation.webservlet;
import jakarta.servlet.http.httpservlet;
import jakarta.servlet.http.httpservletrequest;
import jakarta.servlet.http.httpservletresponse;

import java.io.ioexception;

@webservlet("/myservlet")
public class myservlet extends httpservlet {

    myservlet(){
        super();
    }

    @override
    protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        super.doget(req, resp);
    }

    @override
    protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        super.dopost(req, resp);
    }
}

pom.xml

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>org.example</groupid>
  <artifactid>baseproject</artifactid>
  <packaging>war</packaging>
  <version>1.0-snapshot</version>
  <name>baseproject maven webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>jakarta.servlet</groupid>
      <artifactid>jakarta.servlet-api</artifactid>
      <version>6.0.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalname>baseproject</finalname>
  </build>
</project>

我尝试从 myservlet.java 中删除 '@webservlet("/myservlet")' 并手动将 servlet 和 servlet 映射添加到 web.xml,如下所示:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>Servlets.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>

</web-app>

但是,这仍然导致 500 错误。

解决方法

公开 servlet 的构造函数,以便 tomcat 可以运行它。

public MyServlet() {
    super();
}

以上是如何修复 java.lang.NoSuchMethodException: Servlets.MyServlet.() at java.base/java.lang.Class.getConstructor0的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除