我正在尝试做一个简单的登录功能。我一直收到这个错误:
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,我不确定预期的是什么?程序是否期望classname为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);
}
}
这是我的HTML文件,它是在Apache Tomcat中打开的:
<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>
&#13;
还有一个名为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
答案 0 :(得分:0)
将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>