javascript 如何从jsp调用java函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5049919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a java function from a jsp
提问by Archana
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?
我要求用户在我的项目中使用的用户名与其他已注册的用户名进行比较。用户名应该是不同的。为此,在 newuser.jsp 中获取输入,而 newuser.jsp 又调用 SemanticSearch.java 中的函数 searchForUsername。当新用户注册时,甚至检查电子邮件 ID 以进行验证,稍后在键入用户名时,需要完成上述检查。我试过一种方法,但没有用。请指出我在做什么错误?
My code in SemanticSearch.java has constructor:
我在 SemanticSearch.java 中的代码有构造函数:
public SemanticSearch() {}
The following code follows after validation of email id.
验证电子邮件 id 后遵循以下代码。
My code in newuser.jsp is
我在 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();
}
During the click event of adduser button this function has to be called. But during the click function nothing seems to happen. Please help.
在 adduser 按钮的单击事件期间,必须调用此函数。但是在点击功能期间似乎什么也没有发生。请帮忙。
采纳答案by Vipin
<%
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
我猜您可以将 Java 变量分配给 JavaScript 变量
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.
我更喜欢 BalusC 的方法(第一个),这种情况下的 javascript 验证可能有风险,因为大多数浏览器都支持禁用 javascript。
回答by BalusC
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.
您正在混合 Java 和 JavaScript 语言。那是不正确的。它们都是在不同环境中运行的不同语言。Java 在 webserver 中运行,而 JavaScript 在 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.
创建一个侦听特定 URL 的 servlet,执行验证工作,然后让您的表单提交给它,让 servlet 返回到使用 JSP/EL 显示错误消息的同一页面。您可以在我们的servlet wiki 页面中找到一个简单的示例。
创建一个侦听特定 URL 的 servlet,执行验证工作,然后让您的 JavaScript 代码通过 Ajax 技术调用它并相应地修改 HTML DOM 以添加错误消息。你可以在这个问题的答案中找到一个例子。
回答by Zimbabao
Your code should looks like this. All java code in jsp file need to be enclosed with "<%" and "%>"
您的代码应如下所示。jsp文件中的所有java代码都需要用“<%”和“%>”括起来
<%
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();
<%
}
%>

