在不使用 javascript 的情况下单击 html 按钮时调用 java 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18226840/
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
call a java method when Click on a html button without using javascript
提问by ofloflofl
i work on JSP
and i want to call a java method(Function) on Click on a html button without using<script></script>
.how?
i try to write this code:
我继续工作JSP
,我想在不使用<script></script>
.how 的情况下单击 html 按钮时调用 java 方法(函数)?我尝试编写此代码:
<button onclick="<%po.killThread();%>">
<font size="4">Kill</font>
</button>
but it doesn't work... so please help me.
但它不起作用......所以请帮助我。
thanks
谢谢
回答by calaedo
This will call the function killThread when you open the website.
这将在您打开网站时调用函数 killThread。
Try to redirect to another jsp which calls the function.
尝试重定向到另一个调用该函数的jsp。
回答by Prasad Kharkar
If you are using JSPs, then to perform some method calles, you will have to write a servlet and then call the method in doPost
or doGet
method of servlet.
如果您使用的是 JSP,那么要执行一些方法调用,您必须编写一个 servlet,然后调用 servlet 中的方法doPost
或doGet
方法。
On the other hand, if you want to make things simpler, use JSF
framework which will help you achieve your objective as JSF
supports event handling.
另一方面,如果您想让事情变得更简单,请使用支持事件处理的JSF
框架来帮助您实现目标JSF
。
回答by devrobf
You're misunderstanding how server-side programming works. When you load that page, the webserver will get to the line <button onclick="<%po.killThread();%>">
and will immediatelyparse and execute the JSP snippet, in your case po.killThread()
, and replace everything between the <%
and %>
with the return value of that method, if any. And all these happens on server side, before client receives any thing. (Note that this will only happen if that page is not already been loaded and compiled into a Servlet by the server.)
您误解了服务器端编程的工作原理。当装载页面,Web服务器将获得该行 <button onclick="<%po.killThread();%>">
并立即解析并执行JSP代码片段,在你的情况po.killThread()
,以及之间的取代一切<%
,并%>
与该方法的返回值,如果有的话。所有这些都发生在服务器端,在客户端收到任何东西之前。(请注意,这仅在服务器尚未加载该页面并将其编译为 Servlet 时才会发生。)
Thus, the HTML that client receives, will be something like, <button onclick="some return value or nothing">
, which means that nothing will happen when you press the button. If you want to execute further JSP commands on the button press you will need to make a new request to the server - for example, by redirecting the page.
因此,客户端收到的 HTML 将类似于, <button onclick="some return value or nothing">
,这意味着按下按钮时不会发生任何事情。如果您想在按钮上执行进一步的 JSP 命令,您将需要向服务器发出新请求 - 例如,通过重定向页面。
回答by SpringLearner
this will not run at all because after the jsp page is compiled it will return the po.killThread() value but will not call this method
这根本不会运行,因为在编译jsp页面后,它将返回po.killThread()值但不会调用此方法
You can see this by viewing the page source
您可以通过查看页面源看到这一点
回答by Adeel Ansari
JSP is a server-side technology. Did I say server-side?
JSP 是一种服务器端技术。我说的是服务器端吗?
In order to understand how JSP works and to clear any misconception, JavaRanch Journal (Vol. 4, No. 2): The Secret Life of JavaServer Pagesis a very good read.
为了了解 JSP 的工作原理并消除任何误解,JavaRanch Journal(第 4 卷,第 2 期):JavaServer Pages 的秘密生活是一本非常好的读物。
An excerpt from the same,
摘自同一个,
- JSP is a templating technology best-suited to the delivery of dynamic text documents in a format that is white-space agnostic.
- Template text within a JSP page (which is anything that is not a dynamic element), to include all white-space and line terminators, becomes part of the final document.
- All dynamic elements in a JSP are interpreted on the server and once the document is sent to the client, no further dynamic interaction is possible (short of requesting the same or another document).
- JSP 是一种模板技术,最适合以与空白无关的格式交付动态文本文档。
- JSP 页面中的模板文本(它是任何非动态元素),包括所有空白和行终止符,成为最终文档的一部分。
- JSP 中的所有动态元素都在服务器上进行解释,一旦将文档发送到客户端,就不可能进行进一步的动态交互(除了请求相同或另一个文档)。