calling java method in javascript [closed]

As javascript is a client side script, it cannot invoke java methods directly which resides on the server

Without any particular java frameworks, you could make use of Java Server Pages (JSP) to invoke deleteconfig.initiate() when it receives a GET request from javascript.

Sending Request

You might also want to use JQuery (a javscript plugin - http://jquery.com/) to send an asynchronous GET request to the server like this

//javascript code
function callInititiate(){

   //This sends a get request to executeInit.jsp
   //
   $.get('localhost/myWebbApp/executeInit.jsp');

}

$(callInitiate);

Receive Request

On the server side, you should have executeInit.jsp that calls deleteconfig.initiate() static method

//in executeInit.jsp
<%@ page import="deleteconfig"%>

<%
// executes initiate() static method
deleteconfig.initiate();

%>

Perhaps reading more about Java Server Pages can get you started!


javascript runs in your browser, your java code is deployed in your container(Tomcat).

So only way to invoke this is via a Web call. Your javascript should invoke an ajax call to one servlet(configured in web.xml) and this servlet should invoke your java method.

You can run javascript in server as well.See NodeJS