3

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);
    }
}
Brighid McDonnell
  • 4,293
  • 4
  • 36
  • 61
Farrukh Munir
  • 31
  • 1
  • 1
  • 2

4 Answers4

2

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)

1

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

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
1

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

Sina Jahan
  • 113
  • 1
  • 6
1

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

DwB
  • 37,124
  • 11
  • 56
  • 82