2

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

3
  • 2
    this will not run at all because after the jsp page is compiled it will return the po.killThread() value but ill not call this method Commented Aug 14, 2013 at 8:37
  • You should have a look at JSF and it's ActionListener paradigm. Using JSP scriptlets for these sort of things just creates messy code Commented Aug 14, 2013 at 8:39
  • Is there any reason you don't want to use Javascript? The above will never work, as, as others have stated, the JSP is pre-compiled before the page is displayed. Can you outline the problem a little more please? Commented Aug 14, 2013 at 9:26

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I do not understand what the phrase " will immediately parse and execute the JSP command po.killThread(), and remove everything between the <% and %>" means. If it executes the command, why you say it removes everything between <% and %>? Can you explain it a little bit more detailed?
The main thing to bear in mind is that the client (i.e. a web browser) does NOT understand JSP (or PHP, ASP etc), it must be given pure HTML/Javascript. Thus technologies such as JSP are just HTML preprocessors - they output plain HTML without JSP commands. Read a tutorial on JSP and I'm sure it can explain better than me.
@ jazzbassrob: I have modified your post, as you can see, to make it a little elaborative. You can always revert it, if you don't like. I hope I didn't ruin your answer.
You've edited it to make it very abrupt and rude at the beginning, so I'm going to change that back!
@jazzbassrob: As you wish, it's your answer :).
1

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

Try to redirect to another jsp which calls the function.

Comments

1

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

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.