想要在HTML onclick(jsp)中调用Javabean方法
对于学校作业,我必须使用 Java bean 从 JSP 页面建立到数据库的连接,目前为止该连接有效。 每次我按下这个按钮时,我想在我的bean中调用一个方法..
<INPUT TYPE=SUBMIT VALUE="To DB!" OnClick="<% DBbean.InsertStatement(); %>" style="width:100%; height:50px ">
但这不起作用..有人知道我如何解决这个问题吗? 提前致谢。
For a school assignment I have to make a connection to a database from a JSP page using a Java bean, which works so far.
Everytime I press this button I want to call a method in my bean..
<INPUT TYPE=SUBMIT VALUE="To DB!" OnClick="<% DBbean.InsertStatement(); %>" style="width:100%; height:50px ">
Only this doesn't work.. does anyone have an idea how i can solve this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一些建议:
如果必须,请使用 JSTL
标记或#include
您选择的 bean 并调用其方法。但我不会推荐它。I have some advice:
If you must, use JSTL
<sql>
tags or#include
the bean of your choice and call its methods. But I wouldn't recommend it.了解客户端和服务器端代码之间的区别。当您单击网页上的按钮时,会触发客户端上发生的事情。当您发出请求(在浏览器地址栏中输入 URL、单击链接、发布表单)时,将触发服务器上发生的事情。两者是完全分开的。
为了实现您的建议,您的页面上需要有一个表单 (
编辑:因为这似乎是您正在学习的当前课程元素的要点,如果您遇到困难,您应该向您的老师/导师寻求一些指导。
Understand the difference between client-side and server-side code. When you click a button on a web-page, that triggers something happening ON THE CLIENT. When you make a request (enter a URL in the address-bar on the browser, click a link, post a form) that will trigger something happening ON THE SERVER. The two are completely separated.
To achieve what you are proposing, you will need to have a form on your page (
<form>
) that posts to a servlet on your server. In that servlet, you connect to the database and do whatever before returning a new page to the browser.EDIT: as this seem to be pretty much the gist of the current course element you are taking, you should ask your teacher/tutor for some guidance if you are stuck on this.