Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

servlet abort multipart form data submission after validation fail

I have one jsp page which allows the user to submit a multipart form data to the servlet. This multipart form has Order Id field and another field for uploading a video of 1GB. Now at the server, I perform validation on Order Id to check if order Id is valid or not, If order id is invalid I just return from doPost method. Now the problem is even when I return from doPost this multipart form data keep on sending multipart video file unless it uploads the whole file to a servlet. I just want to abort this Multipart form submission as soon as validation fails on order id without waiting for uploading the whole video file to a servlet. I tried to use the abort method but eclipse didn't show any suggestion.

like image 857
ashish pandey Avatar asked Dec 04 '18 05:12

ashish pandey


2 Answers

There are two solutions you can try:

  1. Validate the OrderId on the first line of the method before it processes the file further and return there if invalid. And also you can add file size validations using @MultipartConfig(fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5). I have tried it, this is working!
  2. Add a web service which validates OrderId onBlur event. This would check if orderId is valid or not. It will reduce the form being submitted initially. This could be done by using AJAX on page.

If these does not help, please share your code snippet so I could analyse it further.

like image 116
Muhammad Ahmed Avatar answered Nov 01 '22 07:11

Muhammad Ahmed


You can try something like this wherein you first fire an AJAX to verify OrderID and if your servlet returns 'true', you proceed with uploading your video. Else you ignore the next operation/s.

var xhttp1 = new XMLHttpRequest();
xhttp1.open("POST", "YOUR_SERVLET", false);
xhttp1.onreadystatechange = function () {
    if (xhttp1.readyState == 4) {
        if (xhttp1.status == 200) {
            if (xhttp1.responseText == 'true') {

                // SEND YOUR 1 GB FILE
                var xhttp2 = new XMLHttpRequest();
                xhttp2.open("POST", "YOUR_SERVLET", false);
                xhttp2.onreadystatechange = function () {
                    if (xhttp2.readyState == 4) {
                        if (xhttp2.status == 200) {
                            if (xhttp2.responseText == 'true') {
                                // YOUR VIDEO UPLOADED SUCCESSFULLY
                            }
                        }
                    }
                };
                xhttp2.setRequestHeader("Content-type", "multipart/form-data");
                xhttp2.send(formData);


            } else {
                // ORDER ID INVALID
            }
        }
    }
};
xhttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp1.send("OrderId=1");
like image 28
hiren Avatar answered Nov 01 '22 07:11

hiren