Skip to content Skip to sidebar Skip to footer

Upload Image And Information From Textfields In The Same Servlet

this is my first time working with JSPs and servlets. I have a form in my JSP that has text input for name, description, etc of a product, and a field to upload an image. Everythin

Solution 1:

use FileItem#getFieldName() to get name of input field and FileItem#getString() for value of input field.

Answer :

create two utilities methods one for regular input fields and second for File input field data. like:

// Process a regular form fieldprivatevoidprocessFormField(FileItem item){
  if (item.isFormField()) {
     String name = item.getFieldName(); 
     String value = item.getString();
  }
}

// Process a file uploadprivatevoidprocessUploadedFile(FileItem imageFile){
   if (!imageFile.isFormField()) {
      //your existing file upload code.
   }
}

then, use above methods inside while loop to get data like:

while (iterator.hasNext()) {
FileItem item = iterator.next();

    if (item.isFormField()) {
        processFormField(item);
    } else {
        processUploadedFile(item);
    }
}

Source

Post a Comment for "Upload Image And Information From Textfields In The Same Servlet"