I was looking how to add image to the form then I found These codes. While integrating this code I failed because I have other fields in the form to be posted when the form is submitted.
So can you please help me to post for instance a field Lastname along with the file and show how can I get it it( Block of code in the servlet to get the lastname) using the same servlet.
You’re mixing Apache Commons FileUpload and Servlet 3.0
@MultipartConfig. Those two are entirely distinct ways to parsemultipart/form-datarequests. A HTTP request can be parsed only once. So if one of those two ways has already parsed it beforehand, the other way would not be able to parse it anymore and end up with null/empty data.You should use the one or the other way to parse the request and not both ways. Apache Commons FileUpload was the "de facto" standard to parse
multipart/form-datarequests before Servlet 3.0 was introduced (Dec 2009). But since Servlet 3.0 there’s the new@MultipartConfigannotation and the newrequest.getPart()method which makes Apache Commons FileUpload superfluous.When using Apache Commons FileUpload, you should remove the Servlet 3.0
@MultipartConfigand allrequest.getParameter()lines and extract the "regular" request parameters from theList itemsinstead.When using Servlet 3.0
@MultipartConfig, you should remove all code related to Apache Commons FileUpload and userequest.getPart()instead to obtain the uploaded file.See also: