2

I want to post a form which have many fields, one of them is a file, when I make like this

<form action="MediaManagementServlet" name="addMedia" method="post" enctype="multipart/form-data">
//    many fileds
<label>upload file</label>
<input type="file" name="file" id="fileName"/>

it doesn't reach the servlet, but when I make like this

<form action="MediaManagementServlet" name="addMedia" method="post" >

it reach the servelet, but when I get the parameter of file it prints null ?

where's the point that is hidden for me?

palAlaa
  • 9,500
  • 33
  • 107
  • 166

1 Answers1

3

It should definitely reach the servlet in both cases. You're likely not running the code you think you're running or are misinterpreting the results or have some JavaScript which takes over the submit but doesn't treat it correctly. Save, rebuild, redeploy and restart everything. The getParameter() only returns null for all fields in 1st case and for only the file in the 2nd case.

As to the null parameter issue, when using multipart/form-data encoding, the data is not been sent as a standard application/www-form-urlencoded query string in the request body. Instead, it's been sent as multipart/form-data block in the request body. The getParameter() and consorts doesn't recognize this and hence they all return null. You need to use getParts() instead.

Collection<Part> parts = request.getParts();
// Loop through it and collect manually.

Or when you're still on old Servlet 2.5 or before, then you need to use Apache Commons FileUpload to parse it.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Depends on make/version of the target runtime (servletcontainer). E.g. Tomcat 7, Glassfish 3, JBoss AS 6, etc all supports Servlet 3.0. If they do, then you should make sure that your `web.xml` is also declared conform Servlet 3.0. This way the `getParts()` is available. – BalusC May 18 '11 at 19:07
  • yes , here is it , Ok I'll try now. – palAlaa May 18 '11 at 19:08
  • @BalusC, I need to restart every thing :(, I tried it, I get all the names of parts in the form,but how can I get the values? – palAlaa May 18 '11 at 19:45
  • It's available by `Part#getInputStream()`. Click the "See also" link for an example. I do not post those links for decoration only, they really contain useful background information :) I must however personally admit, using Apache Commons FileUpload is less verbose than using the Servlet 3.0 API. FileUpload offers convenience methods to get the value as `String` directly and so on. Again, click the "See also" link for the example. – BalusC May 18 '11 at 19:46
  • @BalusC, I tried many things, InputStream used for files, I want to get usual parameters in servlet ?? the form contains many info, rather than the file, do u get what I mean? InputStream doesn't print the value of text fields – palAlaa May 18 '11 at 20:14
  • Click here --> http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824. It contains an example how to convert `InputStream` to `String`. Reread my previous comment once again. – BalusC May 18 '11 at 20:23