简体   繁体   中英

Reading a input email address from html and using in JSP scriplet

Hi I have an html field that takes an email. I would like to take the entered value and ensure it exists in the database before proceeding.

<script>
function updateUserData()
{
document.getElementById(picker_email).value;
alert( "INVALID EMAIL ");
}
</script>

Is there anyway I can pass the picker_email to the JSP...so the outcome would be :

...

value = document.getElementById(picker_email).value;
<%
DatabaseHelper db_h = new DatabaseHelper();
boolean email_exists = db_h.verifyEmail( value );
%>

if( <%email_exists%> )
   proceedToServlet();
else
   alert( "INVALID EMAIL ");

Any help would be greatly appreciated.

@user2747139 developerwjk is correct. Why don't you try some ajax call to server. Writing scriptlet in jsp (Especially for server oriented purpose) is not a good practice. Here is some snippet. All you need to do is include jquery plugin in your jsp page. You can try like this,

function updateUserData(){ 
 var value = $("#picker_email").val();  
 $.ajax({
 url: "ur_servlet_url&value="+value,              
 type: "POST", 
 success: function(data){

  //If you want to return anything in jsp.
alert("Invalid Email");
   } 
 });
}

You do your validation in server side. If the validation succeeds/not succeeds return some text like success or failure . Based on this you will get response data in ajax. You can do alert if(data == 'failure') then alert('Invalid Email'); . Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM