在不刷新页面的情况下提交JSP后发出javascript警报



我正在将数据输入jsp页面并从数据库验证它。如果验证为假,我需要在JSP页面上发出警告,而不刷新页面,因为在刷新页面时,用户被迫重新输入所有详细信息:我的验证方法:

public boolean accountCifMismatch(String account, String custid) {
        Connection conn;
        int count = 0;
        try {
            conn = db.getDbConnection();
            String sql = pr.getDBProperty().getProperty("com.crb.accountCifMismtach");
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            ps.setString(2, custid);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
            DBConnection.closeConn(conn);
            System.out.println(MemoryListener.getMemoryDetails());
        } catch (Exception asd) {
            System.out.println(asd.getMessage());
            return false;
        }
        return count == 0;
    }

My servlet call:

Fraud fmd = new Fraud();
 if (!fmd.accountCifMismatch(account_no, cust_id)) {
//Continue Processing
} else {
          session.setAttribute("accountcifmismtach", true);
          session.setAttribute("content_page", "fraud.jsp");
             }

fraud.jsp我调用javascript:

<script type="text/javascript">
    if (${accountcifmismtach == 'true'}) {
        alert("Account Number CIF Mismtach");
    }
</script>

编辑我正在提交表单:

 <form id="form1" name="form1" method="post" action="do?MOD=BOK&ACT=doFindFraud">
</form>

警报显示,然后页面刷新,因此用户必须再次输入所有详细信息。如何在不刷新页面的情况下显示警告?

如果您想在不刷新页面的情况下验证表单,则需要使用AJAX。

有很多方法可以进行ajax调用,但是对于Java编程语言,我更喜欢DWR(直接Web远程)

DWR Way:

http://directwebremoting.org/dwr/documentation/index.html

其他著名的方法如下:

JQUERY方法

 // process the form
        $.ajax({
            type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
            url         : 'process.php', // the url where we want to POST
            data        : formData, // our data object
            dataType    : 'json', // what type of data do we expect back from the server
                        encode          : true
        })
            // using the done promise callback
            .done(function(data) {
                // log data to the console so we can see
                console.log(data); 
                // here we will handle errors and validation messages
            });

JavaScript(不需要Jquery)

function AJAXPost(formId) {
    var elem   = document.getElementById(formId).elements;
    var url    = document.getElementById(formId).action;        
    var params = "";
    var value;
    for (var i = 0; i < elem.length; i++) {
        if (elem[i].tagName == "SELECT") {
            value = elem[i].options[elem[i].selectedIndex].value;
        } else {
            value = elem[i].value;                
        }
        params += elem[i].name + "=" + encodeURIComponent(value) + "&";
    }
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else { 
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("POST",url,false);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
    return xmlhttp.responseText;
}

在alert之后写入return false,并在javascript中聚焦所需的输入表单元素

交货:docuemnt.getElementById (requiredInputElement) .fous;

最新更新