php小編子墨為您解答修復"java.lang.NoSuchMethodException: Servlets.MyServlet.
我在 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.