admin管理员组文章数量:1291335
I require the username used by the user in my project be pared to other usernames already registered. The username should be distinct. For this the input is taken in newuser.jsp which in turn calls a function searchForUsername in SemanticSearch.java. When the new user is registering even the email id is checked for validation and later when the username is typed the above checking needs to be done. I have tried one way which is not working.Please point what mistake I am doing?
My code in SemanticSearch.java has constructor:
public SemanticSearch() {}
The following code follows after validation of email id.
My code in newuser.jsp is
SemanticSearch myclass=new SemanticSearch();
boolean rets=myclass.searchForUsername(username);
if (rets==false)
{
alert("Username already exists");
document.getElementById("username").value="";
document.getElementById("password").value="";
document.getElementById("username").focus();
}
During the click event of adduser button this function has to be called. But during the click function nothing seems to happen. Please help.
I require the username used by the user in my project be pared to other usernames already registered. The username should be distinct. For this the input is taken in newuser.jsp which in turn calls a function searchForUsername in SemanticSearch.java. When the new user is registering even the email id is checked for validation and later when the username is typed the above checking needs to be done. I have tried one way which is not working.Please point what mistake I am doing?
My code in SemanticSearch.java has constructor:
public SemanticSearch() {}
The following code follows after validation of email id.
My code in newuser.jsp is
SemanticSearch myclass=new SemanticSearch();
boolean rets=myclass.searchForUsername(username);
if (rets==false)
{
alert("Username already exists");
document.getElementById("username").value="";
document.getElementById("password").value="";
document.getElementById("username").focus();
}
During the click event of adduser button this function has to be called. But during the click function nothing seems to happen. Please help.
Share Improve this question edited Feb 19, 2011 at 9:22 Matti Lyra 13.1k8 gold badges52 silver badges67 bronze badges asked Feb 19, 2011 at 9:14 ArchanaArchana 2373 gold badges9 silver badges17 bronze badges 1- it would be better if u post the function searchForUsername(username); – abson Commented Feb 19, 2011 at 9:43
3 Answers
Reset to default 5You're intermixing Java and JavaScript languages. That is not correct. They are both distinct languages which runs in distinct environments. Java runs in webserver and JavaScript runs in webbrowser.
There are basically two ways to achieve the requirement.
Create a servlet which listens on a certain URL, does the validation job and then let your form submit to it and let the servlet return to the same page where error messages are displayed using JSP/EL. You can find a simple example in our servlet wiki page.
Create a servlet which listens on a certain URL, does the validation job and then let your JavaScript code invoke it by Ajax techniques and modify the HTML DOM accordingly to add error messages. You can find an examlpe in an answer of this question.
Your code should looks like this. All java code in jsp file need to be enclosed with "<%" and "%>"
<%
SemanticSearch myclass=new SemanticSearch();
boolean rets=myclass.searchForUsername(username);
if (rets==false)
{
%>
alert("Username already exists");
document.getElementById("username").value="";
document.getElementById("password").value="";
document.getElementById("username").focus();
<%
}
%>
<%
SemanticSearch myclass=new SemanticSearch();
boolean rets=myclass.searchForUsername(username); // how do you get username on button click and assign it to this variable?
if (rets==false)
{
%>
I guess you can assign a Java variable to a JavaScript variable
var username = '<%= username%>';
but does the vice versa work?
<%String username = %>document.getElementById("username").value<%:%>
I would prefer BalusC's approach (1st one), a javascript validation for this case can be risky as mostly all the browsers support disabling of javascript.
本文标签: javascriptHow to call a java function from a jspStack Overflow
版权声明:本文标题:javascript - How to call a java function from a jsp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741501605a2382081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论