I am trying to call a java method in a javascript. java class resides in server side.
The Sample Java Code is:
public class deleteconfig
{
static boolean check = true;
public static void initiate()
{
check = false;
}
}
I would like to call my deleteconfig.initiate() method in my javascript
Any help is greatly appreciated.
Cheers
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
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
Receive Request
On the server side, you should have executeInit.jsp that calls deleteconfig.initiate() static method
Perhaps reading more about Java Server Pages can get you started!
You can use JavaServerPage (JSP) or Applets, here's an example for JSP for your situation:
JSP scripting elements are:
<% = expression%>
The expression is evaluated and printed out the document.<% code%>
The code is inserted into the servlet service method.<%! code%>
The code is inserted into the servlet class, outside of any method.For JSP scripting elements is possible and another syntactic form, based on XML markup:
<jsp:expression> Java expression </ jsp: expression>
<jsp:scriptlet> Java code </ jsp: scriptlet>
<jsp:declaration> Statement Java </ jsp: declaration>
Your Javascript runs client side in the browser so will not normally interact with your Java code running server side. You might want to expose your Java method as a RESTful API endpoint and perform an AJAX call in the Javascript code.
There are a few ways it's technically possible but unsure why you would want to do it that way:
Google's web toolkit: - SEE: http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/DevGuideRPCDeployment