0

Im developing a simple file upload using JSP, everything works fine until I set the form's enctype to "multipart/form-data", request.getParameterNames() is returning empty. Any idea?

Here's my code:

upload.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>File Upload</title>
    </head>
    <body>
        <form method="POST" action="login.jsp" enctype="multipart/form-data">
            <input type="text" name="name" placeholder="File Name"></input>
            <br>
            <input type="file" name="file" id="file"></input>
            <br><br>
            <input type="submit" name="submit" id="submit" value="Submit"></input>
        </form>
    </body>
    </html>

login.jsp

<%@page import="java.util.Enumeration"%>
<%@page import="java.io.InputStream"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            Enumeration params = request.getParameterNames();
            boolean empty = true;
            while(params.hasMoreElements()){
                String param = params.nextElement().toString();
                out.println(param);
                empty = true;
            }
            if(empty) out.println("No parameters received!");
        %>
    </body>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ethyl Casin
  • 791
  • 2
  • 16
  • 34

2 Answers2

1

please see here: Convenient way to parse incoming multipart/form-data parameters in a Servlet

multipart is parsed differently on the servlet side. On servlet spec 3 you need to call 'getParts()', on older specs you might want to consider a 3rd party such as Apache FileUpload .

Community
  • 1
  • 1
Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11
1

This issue is because of enctype="multipart/form-data" so alternatively you can use @MultipartConfig annotation (available after javaee6 and servlet 3.0) or commans-fileupload as suggested in the below posts:

request.getParameter on a submit button giving null value

Get parameter when multipart request in JSP

Community
  • 1
  • 1
Shivang Agarwal
  • 1,825
  • 1
  • 14
  • 19