本文介绍了为什么我得到一个指向ServletFileUpload的HttpServletRequest的NoClassDefFoundError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用JMeter来加载测试我的web应用程序,本地在我的电脑上。我有一个上传图片的jsp页面。图像由我的servlet处理。当我今天尝试这个过程时,我得到了下面的异常/错误:

 异常

javax。 servlet.ServletException:Servlet执行抛出一个异常

根本原因

java.lang.NoClassDefFoundError:javax / servlet / http / HttpServletRequest
org.apache.commons。 fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
spyder.servlets.imageProcessing.ImageProcessingServlet.uploadEditedImagesToDB(ImageProcessingServlet.java:527)
spyder.servlets.imageProcessing.ImageProcessingServlet.doPost(ImageProcessingServlet。 java:153)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
code
$ b $ p
$ b $ p

pre> boolean isPart = ServletFileUpload.isMultipartContent(req);

....而且,我已经把所有必要的 import类中的语句。

我不记得对我的系统上的任何东西进行任何更改都会导致这个问题。这个过程总是没有任何问题,所以我不明白是什么原因导致它现在失败。它是相当巧合的,我认为,这是失败后,我已经使用JMeter ... ... $ / $>

解决方案

所有第三方webapp像Commons FileUpload这样的库属于你的web应用程序的 / WEB-INF / lib ,而不是其他地方。每当你把它放在 JRE / lib JRE / lib / ext 中时,就会发生这个异常。



事实上,正如Bozho提到的那样,您还需要确保您没有移动/复制/复制任何特定于servletcontainer的库(它们应该保留在 Tomcat / lib )在classpath的不同位置。但是,国际海事组织不应该导致这种例外。这基本上是说,加载FileUpload API的类加载器完全不了解Servlet API。

如果您阅读 JRE / lib JRE / lib / ext 由不同的类加载器( bootstrap )加载,比 Tomcat / lib common )和 / WEB-INF / lib webapp )。引导类加载器不具有关于公共和webapp库的知识。这是相反的。 common 类加载器具有有关 bootstrap 类加载器的知识,而 webapp 类加载器具有关于这两者的知识。由于Servlet API通常由 common 类加载器加载,所以这只能意味着FileUpload API由 bootstrap 加载器加载。这是错误的:)


I recently started using JMeter to load test my webapp, locally on my pc. I have a jsp page for uploading images. The images are processed by my servlet. When I tried the process today, I got the following exception/error :

exception

javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
    org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
    spyder.servlets.imageProcessing.ImageProcessingServlet.uploadEditedImagesToDB(ImageProcessingServlet.java:527)
    spyder.servlets.imageProcessing.ImageProcessingServlet.doPost(ImageProcessingServlet.java:153)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Here is the code from my servlet that the exception is referring to -

boolean isPart = ServletFileUpload.isMultipartContent(req);

....and yes, I've put all the necessary import statements in the class.

I can't recall making any changes to anything on my system that would cause this problem. This process has always run without any problems, so I don't understand what is causing it to fail like this now. Its rather coincidental, I think, that it is failing after I've been using JMeter...

解决方案

All 3rd party webapp libraries like Commons FileUpload belong in /WEB-INF/lib of your webapp, not elsewhere. This exception can occur whenever you've placed it in JRE/lib or JRE/lib/ext.

And indeed, as Bozho mentions, you need to ensure as well that you haven't moved/copied/duplicated any servletcontainer-specific libraries (which should be left untouched in Tomcat/lib) around in different places of the classpath. But that should IMO not have resulted in this kind of exception. It's basically telling that the classloader which loaded the FileUpload API has totally no knowledge about the Servlet API.

If you read the Tomcat classloading HOW-TO, then you'll see that the libraries in JRE/lib and JRE/lib/ext are loaded by a different classloader (bootstrap) than the ones in Tomcat/lib (common) and /WEB-INF/lib (webapp). The bootstrap classloader has no knowledge about common and webapp libraries. It's the other way round. The common classloader has knowledge about the bootstrap classloader and the webapp classloader has knowledge about both. Since the Servlet API is normally loaded by the common classloader, this can only mean that the FileUpload API was loaded by the bootstrap classloader. And this is wrong :)

这篇关于为什么我得到一个指向ServletFileUpload的HttpServletRequest的NoClassDefFoundError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 03:02