title: Exception error for file uploading in java
body: <p>I am using simple code for file upload but i don't know why i am getting this error.I am new in java so i don't have too much idea  </p>
  
  <p>Used Code : -</p>
  
  <pre><code>package net.codejava.upload;
  import java.io.File;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Iterator;
  import java.util.List;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  
  public class UploadServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private static final String UPLOAD_DIRECTORY = "upload";
  private static final int THRESHOLD_SIZE     = 1024 * 1024 * 3;  // 3MB
  private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
  private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB
  
  /**
   * handles file upload via HTTP POST method
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // checks if the request actually contains upload file
      if (!ServletFileUpload.isMultipartContent(request)) {
          PrintWriter writer = response.getWriter();
          writer.println("Request does not contain upload data");
          writer.flush();
          return;
      }
  
      // configures upload settings
      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(THRESHOLD_SIZE);
      factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
  
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setFileSizeMax(MAX_FILE_SIZE);
      upload.setSizeMax(MAX_REQUEST_SIZE);
  
      // constructs the directory path to store upload file
      String uploadPath = getServletContext().getRealPath("")
          + File.separator + UPLOAD_DIRECTORY;
      // creates the directory if it does not exist
      File uploadDir = new File(uploadPath);
      if (!uploadDir.exists()) {
          uploadDir.mkdir();
      }
  
      try {
          // parses the request's content to extract file data
          List formItems = upload.parseRequest(request);
          Iterator iter = formItems.iterator();
  
          // iterates over form's fields
          while (iter.hasNext()) {
              FileItem item = (FileItem) iter.next();
              // processes only fields that are not form fields
              if (!item.isFormField()) {
                  String fileName = new File(item.getName()).getName();
                  String filePath = uploadPath + File.separator + fileName;
                  File storeFile = new File(filePath);
  
                  // saves the file on disk
                  item.write(storeFile);
              }
          }
          request.setAttribute("message", "Upload has been done successfully!");
      } catch (Exception ex) {
          request.setAttribute("message", "There was an error: " + ex.getMessage());
      }
      getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
  }
  }
  </code></pre>
  
  <p>Getting Error :-</p>
  
  <pre><code>HTTP Status 500 - type Exception report
  message description The server encountered an internal error () that
  prevented it from fulfilling this request.
  </code></pre>
  
  <p>exception </p>
  
  <p>javax.servlet.ServletException: Servlet execution threw an exception</p>
  
  <p>root cause </p>
  
  <pre><code>java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
  org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
  
  net.codejava.upload.UploadServlet.doPost(UploadServlet.java:35)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
  </code></pre>
  
  <p>root cause </p>
  
  <pre><code>java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
  java.net.URLClassLoader$1.run(Unknown Source)
  java.net.URLClassLoader$1.run(Unknown Source)
  java.security.AccessController.doPrivileged(Native Method)
  java.net.URLClassLoader.findClass(Unknown Source)
  java.lang.ClassLoader.loadClass(Unknown Source)
  java.lang.ClassLoader.loadClass(Unknown Source)
  org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
  net.codejava.upload.UploadServlet.doPost(UploadServlet.java:35)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
  </code></pre>
  
  <p>note The full stack trace of the root cause is available in the Apache <code>enter code here</code>Tomcat/6.0.20 logs.</p>
  
  <p>Please help me, i am waiting for solution</p>