How to call java method from jsp when html element is clicked?

As the title says, how can I call a java class method from jsp when a specific element is clicked (like an anchor)? (Without reloading the page)

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

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

+4
source share
4 answers

What you want to do is run javascript with an AJAX request when you click on the specified item. This AJAX request is sent to the server, which can then call any Java code that you want.

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

+3
source

As Marko pointed out , you might need to read a few more about client / server separation in web programming. If you want the framework to help you make a remote Java call from Javascript, check out DWR .

+2
source

You can use the Ajax call to do this. Now that the HTML object is clicked, call java-script. Then in JavaScript make the servlet Ajax call 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 pass, you can send several parameters. and RespValue is the response value returned from the servlet. In servelt write the Get Get method and execute your Java code. If you want to return some value, use the function (RespValue), and then delete it.

+1
source

You cannot execute server-side Java code in the client browser.

What you can do is execute a new HTTP request that will perform some actions on the server and return the result of the action.

Given the tone of the question better, read the JSP tutorial. No response to a forum post will explain this better.

0
source

All Articles