参见英文答案 > java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest 4个
我是Tomcat servlet编码的新手,我收到了这个错误:
> java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
-> `servlet-api.jar` is in lib `tomcat_root/lib` dirctory
CATALINA_HOME = C:\tomcat\lib\;
JAVA_HOME = C:\Program Files\Java\jdk1.7.0_01
Path = ....;%JAVA_HOME%\bin;%JAVA_HOME%;...
我很困惑,因为javac正在编译代码而没有任何错误.
这是我正在使用的代码 – 你能告诉导致错误的是什么吗?我不能.
package com.life;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class Task10 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
File destinationDir;
String realPath = getServletContext().getRealPath("/files");
destinationDir = new File(realPath);
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
if (ServletFileUpload.isMultipartContent(request)) {
List items = uploadHandler.parseRequest(request);
}
out.println(ServletFileUpload.isMultipartContent(request));
out.println(realPath);
out.close();
}
catch(Exception ex) {
log("Error encountered while uploading file",ex);
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
java.io.IOException {
doGet(request, response);
}
}
解决方法:
在CMD中试试这个:
SET CATALINA_HOME = C:\Program Files\Apache Software Foundation\Tomcat 7.0;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib;
(在当前版本TOMCAT 7.0)