我们正在java.lang.ClassNotFoundException: HttpServlet
Eclipse 中的 Apache Tomcat 服务器 (v5.5) 上运行一个 JSP 项目。Web-inf 下的 lib 文件夹确实有 servelt-api jar。此外,我们还导入了 javax.servlet.http.* 库。但我们仍然得到这个例外。
package com.bean;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AdminLogin extends HttpServlet{
ArrayList list=new ArrayList();
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
try{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String id=request.getParameter("user");
String pass=request.getParameter("pass");
System.out.println("ID==================>>"+id+"PASS========>>"+pass);
if(id.equalsIgnoreCase("admin")&&pass.equalsIgnoreCase("admin"))
{
response.sendRedirect("AdminPage.jsp");
}
else
{
response.sendRedirect("Failure.html");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
错误堆栈跟踪:
java.lang.NoClassDefFoundError: HttpServlet
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClassCond(Unknown Source)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.ClassNotFoundException: HttpServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1359)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClassCond(Unknown Source)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
java.lang.Thread.run(Unknown Source)
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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">
<display-name>CarPooling</display-name>
<servlet>
<description>
</description>
<display-name>AdminLogin</display-name>
<servlet-name>AdminLogin</servlet-name>
<servlet-class>com.bean.AdminLogin</servlet-class>
</servlet>
<servlet>
<servlet-name>employee</servlet-name>
<servlet-class>com.bean.UserLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdminLogin</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>employee</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
项目文件夹结构: