Skip to content Skip to sidebar Skip to footer

How To Call A Java Class Method In Javascript Using Ajax ?

i have a java class :: package MyPackage; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Stat

Solution 1:

Create a blank JSP (for example, textBoxAjaxResp.jsp) and out put your JSON string from that JSP:

<%

String temp1;
PopulateTextboxobj=newPopulateTextbox();
temp1 = obj.method();
%>

<%=temp1 %>

and hit that JSP using jQuery AJAX call.

$.get("mypath/textBoxAjaxResp.jsp", function (response) {
    //do operation using the response
}, "json");

Solution 2:

you can use Direct Web Remoting library to call java functions using javascript

But I would suggest to write servlet (tutorial is pretty old but good, you should use servlet 3.0 if possible) yourself (instead of using DWR which will have their servlet to write to the response stream also this is very simple requirement so you should write servlet yourself instead of using third party library) and write JSON response directly.

Solution 3:

You might want to look at my answer here Updating contents of a jsp page without refreshing this uses a SpringMVC class which returns a text value from server side to client side using jquery ajax.

Post a Comment for "How To Call A Java Class Method In Javascript Using Ajax ?"