简体   繁体   English

从html读取输入的电子邮件地址并在JSP scriplet中使用

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

Hi I have an html field that takes an email. 嗨,我有一个需要电子邮件的html字段。 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 : 无论如何,我可以将picker_email传递给JSP ...所以结果将是:

... ...

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. @ user2747139 developerwjk是正确的。 Why don't you try some ajax call to server. 为什么不尝试对服务器进行一些Ajax调用。 Writing scriptlet in jsp (Especially for server oriented purpose) is not a good practice. 用jsp(特别是面向服务器的目的)编写scriptlet不是一个好习惯。 Here is some snippet. 这是一些片段。 All you need to do is include jquery plugin in your jsp page. 您需要做的就是在jsp页面中包含jquery插件。 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 . 如果验证成功/ failure返回诸如successfailure类的文本。 Based on this you will get response data in ajax. 基于此,您将获得ajax中的响应数据。 You can do alert if(data == 'failure') then alert('Invalid Email'); 您可以执行if(data == 'failure') alert('Invalid Email');然后alert('Invalid Email'); . Let me know if this helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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