如何从jsp调用java函数
我要求将用户在我的项目中使用的用户名与已注册的其他用户名进行比较。用户名应该是不同的。为此,输入在 newuser.jsp 中获取,而 newuser.jsp 又调用 SemanticSearch.java 中的函数 searchForUsername。当新用户注册时,甚至会检查电子邮件 ID 是否有效,稍后当输入用户名时,需要执行上述检查。 我尝试了一种方法,但不起作用。请指出我犯了什么错误?
我在 SemanticSearch.java 中的代码有构造函数:
public SemanticSearch() {}
验证电子邮件 ID 后执行以下代码。
我在 newuser.jsp 中的代码是
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();
}
在 adduser 按钮的单击事件期间必须调用此函数。但在点击功能期间似乎没有发生任何事情。请帮忙。
I require the username used by the user in my project be compared 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在混合使用 Java 和 JavaScript 语言。这是不正确的。它们都是在不同环境中运行的不同语言。 Java 运行在网络服务器中,JavaScript 运行在网络浏览器中。
基本上有两种方法可以实现该要求。
创建一个 servlet,它侦听某个 URL,执行验证作业,然后让您的表单提交给它,并让 servlet 返回到使用 JSP/EL 显示错误消息的同一页面。您可以在我们的 servlet wiki 页面中找到一个简单的示例。
创建一个监听特定 URL 的 servlet,执行验证工作,然后让 JavaScript 代码通过 Ajax 技术调用它,并相应地修改 HTML DOM 以添加错误消息。您可以在此问题的答案中找到一个示例。
You'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.
您的代码应该如下所示。 jsp文件中的所有java代码都需要用“<%”和“%>”括起来
Your code should looks like this. All java code in jsp file need to be enclosed with "<%" and "%>"
我猜你可以将 Java 变量分配给 JavaScript 变量,
但反之亦然吗?
我更喜欢 BalusC 的方法(第一种),这种情况下的 javascript 验证可能存在风险,因为大多数浏览器都支持禁用 javascript。
I guess you can assign a Java variable to a JavaScript variable
but does the vice versa work?
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.