我正在尝试做一个简单的登录功能。我不断收到此错误:

java.lang.NoClassDefFoundError: ControllerServlet (wrong name: mvc/ControllerServlet)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:760)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2472)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:854)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1274)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)


我的班级名称是ControllerServlet,我不确定期望什么?程序是否期望类名称为mvc / ControllerServlet?如果是这样,我该在哪里更改?

这是ControllerServlet类:

package mvc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class ControllerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        LoginBean bean = new LoginBean();
        bean.setName(name);
        bean.setPassword(password);
        request.setAttribute("bean", bean);
        boolean status = bean.validate();
        if (status) {
            System.out.println("Status is true");
            RequestDispatcher rd = request.getRequestDispatcher("C:/apache-tomcat-8.0.21/webapps/mvc/login-success.jsp");
            rd.forward(request, response);
        } else {
            System.out.println("Status is false");
            RequestDispatcher rd = request.getRequestDispatcher("C:/apache-tomcat-8.0.21/webapps/mvc/login-error.jsp");
            rd.forward(request, response);
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}


这是在Apache Tomcat中打开的HTML文件:



<form action = "ControllerServlet" method = "post">
Name <input type = "text" name = "name"> <br>
Password <input type = "password" name = "password"> <br>
<input type = "submit" value = "login">
</form>





还有一个叫做LoginBean的类,它具有用于名称和密码的getter和setter函数,以及一个用于比较密码以进行身份​​验证的布尔函数。

这不是我自己的程序,我正在尝试从另一个网站上获取代码,以尝试了解它的工作方式。那么,我应该改变什么?

这是文件结构:

webapps/ mvc/ META-INF/ MANIFEST.MF WEB-INF/ index.jsp login-success.jsp login-error.jsp web.xml classes/ ControllerServlet.class LoginBean.class

最佳答案

web.xml添加到定义Servlet及其映射的程序中,例如:

<?xml version="1.0" encoding="UTF-8" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>mvc-web-app</display-name>

    <servlet>
        <servlet-name>ControllerServlet</servlet-name>
        <servlet-class>mvc.ControllerServlet</servlet-class>
    </servlet>

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

</web-app>

关于java - 如何修复java.lang.NoClassDefFoundError?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44019166/

10-11 01:00