3

I need to upload files to a servlet running on tomcat. In addition to the file, I want to allow the user to add a comment associated with the file uploaded. I tried the following but it did not work:

<form action='MyUploadServlet' enctype='multipart/form-data' method='POST'>
    <input type='file' name='filechooser'><br />
    <textarea name='comment' cols='15' rows='5'></textarea>
    <input type='Submit' value='Upload'><br />
</form>

Here is a snippet from the server side code:

@WebServlet("/MyUploadServlet") 
public class MyUploadServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        {
            ServletContext sc = request.getServletContext();
            String comment = (String)request.getParameter("comment");
            ....etc}

The omitted part of the server code deals with receiving the contents of uploaded file.

The request.getParameter("comment") line above returns null. I use several servlets and multiple forms in my app. In all other places, if I use request.getParameter("form-input-name"), I always get the value of the corresponding input field. This is the only exception, namely when the form has an input field of type "file".

How do I pass in a comment along with the file upload submit?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
adaj21
  • 543
  • 3
  • 11
  • 25
  • I don't know about the Java part, but in general, this should work. (Want to add some language specific tags?) – Pekka Dec 26 '11 at 18:36
  • 1
    I think it's better for you to paste your servlet code here since there is no problem in this html form. – zuo Dec 26 '11 at 18:37
  • the html code definitely has nothing wrong in it take a look in here: http://www.javadb.com/get-request-parameters-in-a-servlet this is an example which should work... – Mr. BeatMasta Dec 26 '11 at 18:59
  • I edited the original post and provided the server side snippet. As you can see, it is plain vanilla servlet. The file selected by the html snippet above does upload properly, the plumbing does work correctly, e.e. everything works as expected, the only missing thing is the value of comment textarea. – adaj21 Dec 27 '11 at 01:44

1 Answers1

4

You need to get the text field value by the same API as you used to get the content of the uploaded file.

You have specified the HTML form to send the data in multipart/form-data encoding instead of the standard application/x-www-form-urlencoded encoding. The multipart/form-data encoding is mandatory in order to send the file's content along with the form submit. However, the getParameter() method works in combination with application/x-www-form-urlencoded only.

A multipart/form-data request is usually to be parsed with a multipart/form-data parser such as the well known Apache Commons FileUpload, which is a de facto standard in this area. However, since Servlet 3.0 (which you seem to be actually using, given the presence of the also in Servlet 3.0 introduced @WebServlet annotation), there's a new getParts() method which allows you to extract the necessary submitted data using the standard methods without the need for Apache Commons FileUpload. It's however still only a bit more verbose than with Apache Commons FileUpload. You can find a concrete example of the both approaches in this answer: How to upload files to server using JSP/Servlet?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC for the excellent answer and the link. This is precisely what I needed to fix my functionality. – adaj21 Dec 31 '11 at 06:12