java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 CMD 中尝试使用此方法:
(当前版本 TOMCAT 7.0)
Try with this in CMD:
(IN current version TOMCAT 7.0)
也许这是因为您的 CATALINA_HOME 指向 lib 文件夹。
Maybe that is because your CATALINA_HOME is pointing to the lib folder.
我怀疑你的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
您使用的 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 onTomcat6
and inside/common/lib
and/server/lib
onTomcat 5.5
. Make sure you are not using server specific libraries in the webapp/WEB-INF/lib
else it would lead to collision