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.() 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
hello world!
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
#4.0.0 org.example baseproject war 1.0-snapshot baseproject maven webapp http://maven.apache.org junit junit 3.8.1 test jakarta.servlet jakarta.servlet-api 6.0.0 provided baseproject
我嘗試從 myservlet.java 中刪除 '@webservlet("/myservlet")' 並手動將 servlet 和 servlet 映射新增到 web.xml,如下所示:
Archetype Created Web Application MyServlet Servlets.MyServlet MyServlet /MyServlet
但是,這仍然導致 500 錯誤。
公開 servlet 的建構函數,以便 tomcat 可以運行它。
public MyServlet() { super(); }
以上是如何修正 java.lang.NoSuchMethodException: Servlets.MyServlet.