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?