Reputation: 205
I want to trigger a function when I click on a button in jsp file. I write this code:
<%!public void PrintOut(){
System.out.println("okk");
} %>
<body>
<button onclick="PrintOut()"> print OK</button>
</body>
but it doesn't work. and when I use the
`<script>
function myFunction(){<%
System.out.println("OKK");%>}
</script>`
it execute before I click on button.
Upvotes: 1
Views: 18767
Reputation: 691715
Here's how a JSP works:
That little story to explain you that JavaScript and Java are two different languages, and that the Java code is executed on a server, long before the JavaScript code is executed on the browser. What you're doing makes no sense at all. If you want something to happen on the server in San Francisco when a button is clicked in the browser in Paris, then you must send an HTTP request to the server (by submitting a form, or by using AJAX).
Upvotes: 11