对JSP页面进行Ajax调用

I am making an ajax call to a jsp page like this :

$(document).ready(function () {
        $('#photo').photobooth().on("image", function (event, dataUrl) {
        alert(dataUrl); 
        //alert($('#mygroupuserid'));
        //alert(document.Clickpictures.OwnerId.value);
        //alert(imgdata);
        $.ajax({
            url: 'uploadwebcamimage.jsp',
            type: "POST",
            data: {
                encodeimg: dataUrl,
                OwnerId: document.Clickpictures.OwnerId.value,
                OwnerPhone: document.Clickpictures.OwnerPhone.value,
                mobilepass: document.Clickpictures.mobilepass.value,
                emailpass: document.Clickpictures.emailpass.value,
                mypassword: document.Clickpictures.mypassword.value,
                mygroupuserid: document.Clickpictures.mygroupuserid.value

            },
            error : function(){ 
                alert('Error'); 
            },
            success: function(msg){      
                    alert(msg);
            }
        });

        $("#gallery").show().html('<img src="' + dataUrl + '" >');
        });
  });

Now in that jsp page i have to make decision basis on some flag value like this :

if(flag==true){

    out.println("<script>alert('Face Successfully Detected and Password has been sent');document.location='Main.jsp'</script>");
    }

    else if(flag==false){

    out.println("<script>alert('Sorry,Face Cant Be Detected.Try Again');</script>");

    }%>

How can this be achieved.?As this alert box is not being printed right Now.Please help

You need to read the parameter values and check them against what you're expecting:

String ownerId = request.getParameter("OwnerId");
if("target".equals(ownerId))
{
  out.println("<script>....");
}

And to run the Javascript once it is received in the response on the client side, you must use eval(). And you need to fix that missing semi colon in your first JS script.