3

I just read this great answer from BalusC about how to upload files with the 3.0 Servlet API.

My question is about the use of request.getParameter() for common fields. For example, if my form looks like this :

<form action="/upload" method="post" enctype="multipart/form-data">
    <fieldset>
        <label for="description">File description:</label>
        <input type="text" id="description" name="description" value="" />

        <label for="uploadedFile">File:</label>
        <input type="file" id="uploadedFile" name="uploadedFile" />

        <input type="submit" value="Send" />              
    </fieldset>
</form>

Following what BalusC explained, I should manipulate the InputStream returned by part.getInputStream() to get the content of the description field. Why is that ? I tried to simply call request.getParameter("description"), and it seems to work fine.

I use Tomcat 7.0.20.

Thanks for your help.

Community
  • 1
  • 1
Med
  • 628
  • 9
  • 29

2 Answers2

4

I tried to simply call request.getParameter("description"), and it seems to work fine.

Indeed, this is specified as such in the Servlet 3.0 spec, but this did not work in earlier versions of Glassfish until recently, even though it's the reference implementation. This has been reported as Glassfish issue 16740 and has been fixed in 3.1.2, more than 2 years after the first 3.0 release.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • My bad, I should have tried on GlassFish or another server before asking... Apart the lack of specification, any idea why this is not implemented on other servers? It eases the task. – Med Jun 28 '12 at 04:25
  • I can't disagree you. It definitely eases the task. I have 2 years ago also posted an enhancement request to the Servlet 3.0 spec. Maybe it's in Servlet 3.1. – BalusC Jun 28 '12 at 04:30
  • @BaluC This feature should be available in all containers since Servlet 3.0. – Ramesh PVK Jun 28 '12 at 04:52
  • @Ramesh: you're right! I was blindly basing on the behaviour of Glassfish, the reference implementation. It namely doesn't support it. That seems thus a bug in Glassfish. – BalusC Jun 28 '12 at 12:05
3

It is available since Servlet 3.0 itself. All Servlet 3.0 containers should make the input type parameters available through HttpServletRequest.getParameter().

This is what the Servlet 3.0 Spec says on page 23:

3.2 File upload

...

For parts with form-data as the Content-Disposition, but without a filename, the string value of the part will also be available via the getParameter / getParameterValues methods on HttpServletRequest, using the name of the part.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • Indeed, I just checked the specs too. So in the end, who's to blame? Why is this part of the specs not implemented everywhere? Usually, it's Tomcat that lacks stuff, not the others. I'm lost! :) – Med Jun 28 '12 at 09:18
  • I think all server's should implement it. – Ramesh PVK Jun 28 '12 at 09:36