I have created a hidden form element
<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage">
<label>
</label>
<input name="imgUploadObjId" id="imgUploadObjId" value="52" type="hidden">
//rest of the form here
</form>
And I am trying to get the value with this line in a servlet (as I have done before):
int objId = Integer.parseInt(request.getParameter("imgUploadObjId"));
But I get this (line 33 is the line above):
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
web.objects.UploadImage.doPost(UploadImage.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Is there something different about a form with enctype=”multipart/form-data”? Or can you see some other error.
The servlet parses the parameters by default using
application/x-www-form-urlencodedencoding. Themultipart/form-dataencoding however isn’t supported in servlets until Servlet 3.0. ThegetParameter()calls will all returnnull.In Servlet 3.0, you should have used
HttpServletRequest#getParts()instead to get all parts of amultipart/form-datarequest, including normal form fields. Prior to Servlet 3.0, you should have used Apache Commons FileUpload to parsemultipart/form-datarequests. See also the following answer for a detailed example of both approaches: How to upload files to server using JSP/Servlet?Note that if you aren’t using any
<input type="file">field at all, then you can just leave the encoding away from the<form>. It will then default toapplication/x-www-form-urlencoded.