tags:

views:

1897

answers:

3

As title says, how do I call a java class method from a jsp, when certain element is clicked (by example an anchor)? (Without reloading the page)

If this can be done, how I pass the method, some part of the html code of the page that invokes it?

Im using jsp, servlets, javascript, struts2 and java, over Jboss AS.

A: 

You can't execute server side java code in client browser.

What you can do is to perform new http request that will perform some action on server and return the action result.

Given the tone of the question better go read some JSP tutorial. No forum post answer will explain it better.

Dev er dev
I don't want to execute the java code on the client side. What I want is to execute a java function(on the server side) when an element is clicked on the jsp. I may be newbie as you tagged my question, but if I already know the answer, then I will have not post here. Your answer doesn't helps.
Mg
it's recommended in all forums across the web to READ the question before answering...
SinneR
+3  A: 

What you want to do is have javascript fire off an AJAX request when the said element is clicked. This AJAX request will go to the server which can then invoke any java code you want.

Now you can build this all yourself or you could use one of the many off the shelf solutions. I would recommend Googling around for a JSP Ajax tag library. Like this one http://ajaxtags.sourceforge.net/ .

Rob Mayhew
+2  A: 

As Marko pointed out, you might need to read some more about the client/server separation in web programming. If you want a framework to help you do remote Java invocation from Javascript, have a look at DWR.

Guillaume