This question already has an answer here:
I am using apache-commons-fileupload
to get file from client
to the server
.(using JSP
and Servlet
).
JSP/HTML
<form method="POST" action="GetFile" enctype="multipart/form-data">
<input type="file" name="datafile">
<input type="text" name="text1">
<input type="submit" value="Next">
</form>
Servlet: GetFile
System.out.println(request.getParameter("text1"));
I am able to upload the file to the server, but I am not able to get the value of text1
in the servlet
(I am getting null
value of text1
in the servlet
), I need this textfield
in the form to submit some additional information while uploading it to the server
.
- Is
enctype="multipart/form-data"
option of form doesn't allow other form data to be submited? if it doesn't allow it then what are the other options I have to send this additionaltextfield
to theserver
. - Or is there any other problem in my code?
Above code will read file along with other form data just have a look at
req.getParameter();
method ofMultipartRequest req
objectWell the parameters are not lost , its just that they are part of request Stream.
You have to get all the items from the Request and iterate and handle it accordingly based on their item type
Heres how you can get it
The best practice to retrieve HTML form fields in Servlet is to use apache commons-fileupload 1.3 jar.
Using iterator iterate through multipart HTTPServletRequest and use a for loop to check if it isFormField(), then
and your HTML file should be like this
No there is no issue with using
enctype="multipart/form-data"
. You can get other fields then file in such form.Yes, as for now. While using
enctype="multipart/form-data"
you can not directly get parameters by usingrequest.getParameter(name);
. While using it, form fields aren't available as parameter of the request, they are included in the stream, so you can not get it the normal way. You can find a way to do this in the docs of using commons-fileupload, under the section Processing the uploaded items.com.oreilly.servlet.MultipartRequest
in your servlet / .java file contained inWeb-Inf/classes
in your servlets
doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
method addMultipartRequest m=new MultipartRequest(request, "C:\SavingDirectory");
then call your variables from the form as below;
and print them from the servlet like
out.println(pdate);