-1

I have an Application in Java EE using Apache Commons File Upload API. Everything is going smoothly so far but I have a question.

I have some kind of validation on the server side - if there is any problem I have to send the user to the previous page and inform the problem to the user. Also I have to load the data into the form, but when I use request.getParameter("key") in the JSP, it is returning null.

I have tested with the request not being handled by commons-fileupload and I see the result.

My question is how to keep the request.getParameter("key") and send it back to the client after the request is handled by org.apache.commons.fileupload.servlet.ServletFileUpload?

<%out.println(request.getParameter("cedula"));%> returning null after request being handle by the API

Roman C
  • 49,761
  • 33
  • 66
  • 176
javiut
  • 193
  • 6
  • 24
  • Are you sure that you handle it in the right scope? – Roman C Nov 16 '12 at 13:01
  • Check [this](http://stackoverflow.com/questions/13048939/file-upload-with-servletfileupload-parserequest/13049144#13049144) – Roman C Nov 16 '12 at 13:07
  • Yeah Roman i have test it without request handle before commons and i see the result but when commons handle the request returns null. i need to see the value in a JSP i store all them in request.setAttribute() and i see it in the Jsp i dont know if is a good approach. – javiut Nov 17 '12 at 12:28

2 Answers2

0

That's normal. The whole HTTP request is been sent in a different format which only Apache Commons FileUpload can handle. The request.getParameter("name") won't work for any of the parameters, also not the regular ones.

You should be using the very same Apache Commons FileUpload API to extract the values from regular input fields. You should normally do that when the item.isFormField() line in their code examples returns true (extracting the uploaded file is to be done when it returns false, you've likely already implemented that part).

Alternatively, if you're already on Servlet 3.0 (Tomcat 7, Glassfish 3, etc), then you can also just use the @MultipartConfig annotation on the servlet, so that you can get the uploaded files by request.getPart("name") and continue using request.getParameter("name") for regular input fields.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hello Thanks for response. yeah i have done with item.isFormField() in servlet my question is how do it in a JSP(a little more coding). i have store in request.setAttribute is a good approach? thanks a lot. – javiut Nov 17 '12 at 12:25
  • Hello i have another question. i need to store the file(Picture) on DB. i have handle the file very good my question is. i have to write to Disk(Folder)? or can i direct store in a byte[] without use fi.write(PhotoFile) there is a method like getBytes() which returns me the bytes for i can handle to the DB directly. thanks a lot – javiut Nov 17 '12 at 12:38
  • here is the code to saving in byte[] instead of File Folder before being save to DB.`code`byte[] Array=(IOUtils.toByteArray(fi.getInputStream()));`code` – javiut Nov 17 '12 at 13:56
0

1) Why do you use getParameter instead of getAttribute

2) When validation errors happen the request is redirected, this means a new request object is sent and you need to supply request attributes again.

The same way you did it when the action is dispatched to JSP. If you want to pass the parameter then then you should supply it in the attribute action of the <form> tag.

It seems framework dependent but you could try. Note, in the JSP if you use EL you can try ${param} to access the parameters and ${attr} to access attributes instead if scriplets' getParameter, if you really want to pass it via URL. There's also good art on JSTL.

Roman C
  • 49,761
  • 33
  • 66
  • 176