1

I'm developing an email program using JSP. In that I need to send data as well as upload file.

<form name="email" enctype="multipart/form-data" method="post" action="/servlet/sendmail">
  <input type="file" name="namefile" size="40">
  <input type="text" size="100" name="sub">
  <input type="submit" name="submit" value="send">
</form>

In servlet java program I can upload the file but the text fields returns null.

In doPost() method,

String msg=request.getParameter("sub");

Here getParameter() method returns null for text fields.

Can we include both file type and text in a single form with enctype="multipart/form-data"?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
renu
  • 51
  • 3
  • 6

1 Answers1

2

Yes, that's possible. You should obtain the text field using the same API as you've obtained the uploaded file. It's unclear which one you're using to obtain the uploaded file, so I can't give a detailed answer. But a defacto standard API to parse multipart/form-data requests is Apache Commons FileUpload and I've posted an answer before how to do that right here. To the point, you need to handle cases as well where FileItem#isFormField() returns true. This indicates a "regular" form field.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555