首頁 > 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
發布: 2024-02-09 11:33:18
轉載
534 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板