查询 MySQL 数据库客户端
我正在尝试验证注册表单上的用户名是否唯一,并且希望在客户端键入用户名后立即验证用户名的唯一性,而不是在提交表单后在服务器端执行此操作。
我是否应该从数据库中收集 resultSet,将其存储在数组中,然后以 bean 的形式将其传递到 jsp 页面(我使用模型 2 设计,以便用户在到达 jsp 页面之前通过 servlet )?如果数组很大怎么办?我如何将这些数据带入JavaScript?
或者,有没有办法在客户端使用 ajax 和 javascript 进行查询?也许可以以某种方式在后台运行查询?
我真的只是在寻找一些方向,因为我不知道如何开始研究这样的事情。从性能角度来看,这是否是一个明智之举?
I am trying to validate that a username is unique on a registration form and would like to verify the uniqueness of the username right after the client types it as opposed to performing this server side after the form has been submitted.
Should I collect a resultSet from the database, store it in an array and then pass this along to the jsp page in the form of a bean (I am using a model 2 design so the user passes through a servlet before arriving at the jsp page)? What if the array is very large? How do I bring this data into javascript?
Alternatively, is there a way to do the query using ajax and javascript all on the client side? Maybe its possible to somehow run the query in the background?
I am really just looking for some direction because I am clueless as to what to even begin researching something like this. Is this even a smart move, performance wise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会为此使用“AJAX”。
这是一种方法:在表单的用户名文本字段上设置一个blur()处理程序。当调用blur()方法时,您将用户名发布到后端代码;它会验证它并返回一些适当的响应。然后,您解析响应并更改用户名文本字段上的 CSS 类(例如,将其变为红色)——或者执行任何您想要执行的其他视觉操作以指示“正在使用的用户名”。
无论哪种方式,你都必须从客户端获取用户名到服务器进行验证;您不会想要任何允许客户端直接使用数据库的机制(考虑安全性/漏洞利用等)。
如果您还不熟悉,请查看 jQuery (http://jquery.com/),以使您的客户端生活更加轻松。
I'd use "AJAX" for this.
Here's one approach: set up a blur() handler on the username text field of your form. When the blur() method is invoked, you post the username to the backend code; it verifies it and returns some appropriate response. You then parse the response and change the CSS class on the username text field (e.g., turning it red) -- or do whatever else visually you want to do to indicate "username in use."
Either way, you've got to get the username from the client to the server for verification; you wouldn't want any mechanism which allowed the client to directly use the DB (think security/exploits/etc).
If you're not already familiar, check out jQuery (http://jquery.com/) to make your client-side life much easier.