title: Why is the name of the file I upload null? jsp/servlet
body: <p>Ι upload a photo using the following servlet and it works. But when I print the photo's name in a jsp page it is null. I need it so as to save it in my database. Any ideas?</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  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 FileUploadHandler extends HttpServlet {
      private final String UPLOAD_DIRECTORY = "C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\ismgroup9\\fileUpload";
  
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
      if(ServletFileUpload.isMultipartContent(request)){
          try {
              List&lt;FileItem&gt; multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
  
              for(FileItem item : multiparts){
                  if(!item.isFormField()){
                      String name = new File(item.getName()).getName();
                      item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                      request.setAttribute("photoname", name);
                  }
              }
  
             request.setAttribute("message", "File Uploaded Successfully");
          } catch (Exception ex) {
             request.setAttribute("message", "File Upload Failed due to " + ex);
          }
  
      }else{
          request.setAttribute("message", "Sorry this Servlet only handles file upload request");
      }
  
      request.getRequestDispatcher("/uploadfileController.jsp").forward(request, response);
  
  }
  </code></pre>
  
  <p>}</p>