如何将提示框输入数据传输到servlet?

时间:2012-03-16 05:12:14

标签: javascript jsp servlets

我想将提示框输入数据传输到servlet。 我的过程是调用javascript函数进行表单验证。如果表单数据有效,我会按提示框询问公用名。我想用这个名字转移到servlet,我将通过servlet获得这个名字。 这个过程是真的吗?如果为true,如何将此提示框输入数据传输到servlet?我没有使用Ajax。

ံJSP代码:

<html>
    <head>
    <title>TESTING!!!</title>
    <script type="text/javascript">
    function validateForm(){
     var email=document.frm.email.value;
     var password = document.frm.password.value;
     var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

     var result = true;
     if(email==null || email==""){
     alert("fill email address");
    document.frm.email.focus();
    result = false;
    }else if(file==null || file == "" ){
    alert("choose file");
    document.frm.fName.focus();
    result = false;
    }else if(password==null || password == "" ){
    alert("fill password");
    document.frm.password.focus();
    result = false;
    }else if (reg.test(email)==false){
    alert("Not a valid e-mail address");
    document.frm.email.focus();
    result = false;
    }else{
     var cn = prompt('Common Name', '');
     if(cn) alert("Common Name is " +cn);
     result = true;
    }
    return result;                  
    }
    </script>
    </head>
    <body>
    <form name="frm" action="test" method="POST"
        enctype="multipart/form-data" onsubmit="return validateForm();">
    <p>Email : <input type="text" name="email"></p>
    <p>Password : <input type="password" name="password"></p>
    <p><input type="submit" value="Submit"></p>
    </form>
    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

嘿,你可以试试这个:

例如:

<script language="javascript" type="text/javascript">
    function call(){
       var name = prompt('Common Name', '');
       window.location.replace("a.jsp?name="+name);
    }
</script>

然后从jsp你可以调用sevlet并使用值...

答案 1 :(得分:0)

以下是修改后的代码: *

<html>
    <head>
    <title>TESTING!!!</title>
    <script type="text/javascript">
    function validateForm(){
     var email=document.frm.email.value;
     var password = document.frm.password.value;
     var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

     var result = true;
     if(email==null || email==""){
     alert("fill email address");
    document.frm.email.focus();
    result = false;
    }else if(file==null || file == "" ){
    alert("choose file");
    document.frm.fName.focus();
    result = false;
    }else if(password==null || password == "" ){
    alert("fill password");
    document.frm.password.focus();
    result = false;
    }else if (reg.test(email)==false){
    alert("Not a valid e-mail address");
    document.frm.email.focus();
    result = false;
    }else{
     var cn = prompt('Common Name', '');
     if(cn) alert("Common Name is " +cn);
     document.frm.commonName.value = cn; // assigning propt value to hidden field 
     result = true;
    }
    return result;                  
    }
    </script>
    </head>
    <body>
    <form name="frm" action="test" method="POST"
        enctype="multipart/form-data" onsubmit="return validateForm();">
    <p>Email : <input type="text" name="email"></p>
    <p>Password : <input type="password" name="password"></p>
    <p><input type="submit" value="Submit"></p>
    <!-- hidden field for commonName -->
    <input type="hidden" value="" name="commonName" />
    </form>
    </body>
    </html>