How can I verify the submission of a jsp form request on the same jsp page? (ENCTYPE = "multipart / form data")

<% ..... code to check request is submitted or not ... %> <form method="post" action="" name="uploadform" enctype="multipart/form-data"> <table > <tr> <td>Select up to 3 files to upload</td> </tr> <tr> <td> <input type="hidden" name="newFileName" value="newUploadedFileName1"> <input type="file" name="uploadfile" > </td> </tr> <tr> <td align="center"> <input type="submit" name="Submit" value="Upload"> <input type="reset" name="Reset" value="Cancel"> </td> </tr> </table> </form> 

The jsp request code for downloading files will be on the same page above in the% ..%> tag.

How can I add verification using the if statement to check if the Submit button is clicked or not, in other words, whether the mail request is sent or not.

I followed this, http://www.java2s.com/Code/Java/JSP/SubmittingCheckBoxes.htm , but that didn’t help, and this should call another jsp file ("formAction.jsp") on more than one page.

 if(request.getParameter("newFileName") != null) { 

This instruction always gives null. I do not know why.

Note: this is a form with this attribute => enctype = "multipart / form-data"

If I do not add any if instructions, then the files are downloaded for the first time, but next time, if I press the reload button or refresh the page, the browser will prompt me to resend the data or reload the previous files. I hope you understand the problem. If there is any confusion to understand the problem, please comment.

Can someone give a hint / solution?

+4
source share
1 answer

When using multipart/form-data request is sent in a different encoding than the standard application/x-www-form-urlencoded encoding, which getParameter() and the spouses rely getParameter() . That is why they all return null . You need to analyze the multipart/form-data request for reasonable objects before you can determine the values ​​presented. Apache Commons FileUpload or request.getParts() usually used for this. See this answer for more details.

Alternatively, you can also just check the request method. Normal request (link, bookmark, etc.) Always GET. The submit form for uploading a file is always POST.

 if ("POST".equalsIgnoreCase(request.getMethod())) { // Form was submitted. } else { // It was likely a GET request. } 

But that does not make full sense inside the JSP. Writing Java source code in a JSP file is a recipe for maintenance problems. Rather, submit the form to a fully functional servlet and just implement the doPost() method. In the servlet, you have the freedom to write Java code without regard to this HTML.

+8
source

All Articles