ofloflofl
ofloflofl

Reputation: 205

call a java method when Click on a html button without using javascript

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:

<button onclick="<%po.killThread();%>">
    <font size="4">Kill</font>
</button>

but it doesn't work... so please help me.

thanks

Upvotes: 2

Views: 18035

Answers (5)

Adeel Ansari
Adeel Ansari

Reputation: 39897

JSP is a server-side technology. Did I say server-side?

In order to understand how JSP works and to clear any misconception, JavaRanch Journal (Vol. 4, No. 2): The Secret Life of JavaServer Pages is a very good read.

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

Upvotes: 1

devrobf
devrobf

Reputation: 7213

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 immediately parse 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.)

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.

Upvotes: 3

SpringLearner
SpringLearner

Reputation: 13844

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

You can see this by viewing the page source

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

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.

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.

Upvotes: 0

calaedo
calaedo

Reputation: 333

This will call the function killThread when you open the website.

Try to redirect to another jsp which calls the function.

Upvotes: 1

Related Questions