Reputation: 1467
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.
Upvotes: 4
Views: 6554
Reputation: 1135
You can use a Ajax call to do it . Now when the HTML object is clicked call a java-script. Then in the JavaScript make a Ajax call to the servlet something like this
$.get("Query?ID="+id ,function(RespValue)
{
}
Here Query is my servlet mapping defined in web.xml and Id is the parameter i am passing you can sent multiple parameters too. and RespValue is the response value returned from the servlet. In the servelt write a do Get method and execute your java code. If you want to return some value use the function(RespValue) else remove it.
Upvotes: 1
Reputation: 18895
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.
Upvotes: 2
Reputation: 2724
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/ .
Upvotes: 3
Reputation: 31483
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.
Upvotes: 0