如何从jsp调用java函数

发布于 2024-10-18 07:19:24 字数 753 浏览 1 评论 0原文

我要求将用户在我的项目中使用的用户名与已注册的其他用户名进行比较。用户名应该是不同的。为此,输入在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-10-25 07:19:24

您正在混合使用 Java 和 JavaScript 语言。这是不正确的。它们都是在不同环境中运行的不同语言。 Java 运行在网络服务器中,JavaScript 运行在网络浏览器中。

基本上有两种方法可以实现该要求。

  1. 创建一个 servlet,它侦听某个 URL,执行验证作业,然后让您的表单提交给它,并让 servlet 返回到使用 JSP/EL 显示错误消息的同一页面。您可以在我们的 servlet wiki 页面中找到一个简单的示例。

  2. 创建一个监听特定 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.

  1. 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.

  2. 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.

一梦浮鱼 2024-10-25 07:19:24

您的代码应该如下所示。 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();
<%
}
%>

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();
<%
}
%>
潦草背影 2024-10-25 07:19:24
<%
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)
{
%>

我猜你可以将 Java 变量分配给 JavaScript 变量,

var username = '<%= username%>';

但反之亦然吗?

<%String username = %>document.getElementById("username").value<%:%>

我更喜欢 BalusC 的方法(第一种),这种情况下的 javascript 验证可能存在风险,因为大多数浏览器都支持禁用 javascript。

<%
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文