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

发布于 2024-12-19 23:40:48 字数 2012 浏览 0 评论 0原文

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

I'm very new to Tomcat servlet coding, and I'm getting this error:

> 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%;...

I'm confused, because javac is compiling the code without any error.

Here's the code that I'm using - can you tell what's causing the error? I can't.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静赏你的温柔 2024-12-26 23:40:48

在 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)

Try with this in CMD:

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

(IN current version TOMCAT 7.0)

嘿看小鸭子会跑 2024-12-26 23:40:48

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

Maybe that is because your CATALINA_HOME is pointing to the lib folder.

沐歌 2024-12-26 23:40:48

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

I suspect that your CATALINA_HOME environment variable is wrong. If tomcat is installed in the "c:\tomcat" directory, then you should use the value CATALINA_HOME = C:\tomcat

提笔落墨 2024-12-26 23:40:48

您使用的 Tomcat 版本是什么?您使用的是 Tomcat 附带的 Servlet jar 还是单独下载的? servlet-api.jar 应位于 Tomcat6 上的 /lib 目录以及 /common/lib 和 < Tomcat 5.5 上的 code>/server/lib。确保您没有在 web 应用 /WEB-INF/lib 中使用服务器特定的库,否则会导致冲突

What version of Tomcat are you using? Are you using the Servlet jar shipped alongwith Tomcat or have you downloaded it separately? servlet-api.jar should be present inside /lib directory on Tomcat6 and inside /common/lib and /server/lib on Tomcat 5.5. Make sure you are not using server specific libraries in the webapp /WEB-INF/lib else it would lead to collision

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文