java.lang.NoClassDefFoundError:javax / servlet / http / HttpServletRequest

时间:2011-12-06 18:21:29

标签: java tomcat servlets noclassdeffounderror

我是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);
    }
}

4 个答案:

答案 0 :(得分:2)

在CMD中尝试使用

  

SET CATALINA_HOME = C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0; C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ lib;

(IN当前版本TOMCAT 7.0)

答案 1 :(得分:1)

您使用的是哪种版本的Tomcat?您使用的是随Tomcat一起提供的Servlet jar还是单独下载的? servlet-api.jar位于/libTomcat6目录内,/common/lib/server/libTomcat 5.5/WEB-INF/lib。确保您没有在webapp {{1}}中使用特定于服务器的库,否则会导致冲突

答案 2 :(得分:1)

也许这是因为您的CATALINA_HOME指向lib文件夹。

答案 3 :(得分:1)

我怀疑您的CATALINA_HOME环境变量是错误的。如果tomcat安装在“c:\ tomcat”目录中,那么您应该使用值CATALINA_HOME = C:\tomcat