Bruno Mendes
Bruno Mendes

Reputation: 83

Empty parameters list in requests from .html post multipart/form-data forms to servlets 2.5 or 3.0

My problem is I have a form in an .html page which activates a servlet named CodeSubmission. At first I implemented using the 3.0 API (by just using @WebServlet("/CodeSubmission")), but whenever the servlet was activated the request came without any parameters. So I decided to give it a try and use the 2.5 API in a brand new project (by manually adding the servlet to the web.xml file), but again, whenever the servlet is called the request came without any parameters.

I also thought it was because of file uploading field, but even after removing it the select field doesn't appear as a request parameter too.

Here is the html form code:

<form action="CodeSubmission" method="POST" enctype="multipart/form-data">
<label for="compiler">Compilador:</label>
<select id="compiler">
    <option value="c">C</option>
    <option value="c++">C++</option>
    <option value="scala7">Scala 2.7.7</option>
    <option value="scala8">Scala 2.8.1</option>
    <option value="java5">Java 1.5</option>
    <option value="java6">Java 1.6</option>
</select>
<br /> 
<label for="code">Arquivo:</label>
<input id="code" type="file" size=80 />
<br /> 
<input type="submit" value="Enviar" />
</form>

And here is the test code to check the parameters:

    System.out.println("Length: "+request.getContentLength());
    System.out.println("Content Type: "+request.getContentType());
    System.out.println("Method: "+request.getMethod());
    Enumeration<String> attributeNames = request.getAttributeNames();
    System.out.println("Request Attributes");
    while (attributeNames.hasMoreElements()) {
        String name = attributeNames.nextElement();
        System.out.println(name + ": " + request.getAttribute(name));
    }
    Enumeration<String> paramNames = request.getParameterNames();
    System.out.println("Request Parameters");
    while (paramNames.hasMoreElements()) {
        String name = paramNames.nextElement();
        System.out.println(name + ": " + request.getParameter(name));
    }

The request.getParameterNames() Enumeration has always size 0. Here is what is printed in the console when I click the 'Enviar' button after loading a file and selecting an option, using Servlet 2.5:

Length: 44
Content Type: multipart/form-data; boundary=----WebKitFormBoundary4E3NYbsqXZZMWwcl
Method: POST
Request Attributes
Request Parameters

Upvotes: 1

Views: 2422

Answers (1)

Bozho
Bozho

Reputation: 597382

multipart/form-data has different encoding scheme for parameters. For servelt 2.5 you should use commons-fileupload. With servlet 3.0 you can still use it, or use request.getPart(..). You should also annotate your servlet with @MultiPartConfig

Upvotes: 5

Related Questions