title: Get form value in Servlet
body: <p>I am new to servlet. What I'm trying to do is upload the file to my server using servlet alongside I send one text value which is file name to change in server side. The problem is I submit the form data to servlet using ajax post but in my servlet, I'm getting null value from request.getParameter.</p>
  
  <p>This is my html;</p>
  
  <pre><code>$("#fileUp").html(  "&lt;form enctype='multipart/form-data' id='uploadForm' action='" + url + "/PrjHRService/FileUpload'&gt;"+
                          "&lt;input name='file' id='file' type='file' size='50'&gt;"+
                          "&lt;input name='fname' type='text' &gt;"+
                          "&lt;input id='btnUpload' value='Upload' type='submit'&gt;"+
                          //"&lt;div id='imgLink'&gt;&lt;/div&gt;"+
                          "&lt;/form&gt;" );
  </code></pre>
  
  <p>This is my jquery ajax call to server;</p>
  
  <pre><code>$("#uploadForm").submit(function() {            
              var formData = new FormData($(this)[0]);
              $.ajax({
                     type: "POST",
                     url: $("#uploadForm").attr("action"),
                     data: formData, 
                     async: false,
                     success: function(data)
                     {
                         if(data.res === "true"){
                           jsi.showMsg("Uploaded Successfully");
                           $("#imgLink").html("Uploaded Successfully");
                         }else{
                           jsi.showMsg("Error Uploading");    
                         }
                     },
                     contentType: false,
                     processData: false
                   });
  
              return false; // avoid to execute the actual submit of the form.
          });
  </code></pre>
  
  <p>This is how I get the value from form in my servlet; In there you will see the line String fileExt=request.getParameter("fname"); I found out that fileExt is null which is my problem.</p>
  
  <pre><code>public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
        // Check that we have a file upload request
        isMultipart = ServletFileUpload.isMultipartContent(request);
        response.setContentType("application/json");
        String fileExt=request.getParameter("fname");
        java.io.PrintWriter out = response.getWriter( );
        if( !isMultipart ){
  
           JSONObject o=new JSONObject();
            try {
              o.put("res", "false");
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
            out.println(o.toString());
  
           return;
        }
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // maximum size that will be stored in memory
        factory.setSizeThreshold(maxMemSize);
        // Location to save data that is larger than maxMemSize.
        factory.setRepository(new File("c:\\temp"));
  
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        // maximum file size to be uploaded.
        upload.setSizeMax( maxFileSize );
  
        try{ 
        // Parse the request to get file items.
        List fileItems = upload.parseRequest(request);
  
        // Process the uploaded file items
        Iterator i = fileItems.iterator();
        while ( i.hasNext () ) 
        {
           FileItem fi = (FileItem)i.next();
           if ( !fi.isFormField () )  
           {
              // Get the uploaded file parameters
              String fieldName = fi.getFieldName();
              String fileName = fi.getName();
              //String[] parts = fileName.split(".");
              //fileName = parts[0] + "." + parts[1];
              String contentType = fi.getContentType();
              boolean isInMemory = fi.isInMemory();
              long sizeInBytes = fi.getSize();
              // Write the file
              if( fileName.lastIndexOf("\\") &gt;= 0 ){
                 file = new File( filePath + 
                 fileName.substring( fileName.lastIndexOf("\\"))) ;
              }else{
                 file = new File( filePath + 
                 fileName.substring(fileName.lastIndexOf("\\")+1)) ;
              }
              fi.write( file ) ;
              JSONObject o=new JSONObject();
              o.put("res", "true");
              //out.println("Uploaded Filename: " + fileName + "&lt;br&gt;");
              out.println(o.toString());
              //out.println("Uploaded Successfully");
           }
        }
       /* out.println("&lt;/body&gt;");
        out.println("&lt;/html&gt;");*/
     }catch(Exception ex) {
         System.out.println(ex);
     }
     }
  </code></pre>