calling java method in javascript

102,064

Solution 1

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!

Solution 2

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

Solution 3

You can use JavaServerPage (JSP) or Applets, here's an example for JSP for your situation:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
 <HEAD> 
  <TITLE>JSP</TITLE> 
 </HEAD> 
 <BODY> 
  <H2>JSP </H2> 
  <UL> 
    <LI>Curent time: <%= new java.util.Date() %> 
    <LI>Host name: <%= request.getRemoteHost() %> 
    <LI>ID sesion: <%= session.getId() %> 
  </UL> 
  <% deleteconfig deletecfg = new deleteconfig(); %>
  <%= delectecfg.initiate(); =%>
 </BODY> 
</HTML> 

JSP scripting elements are:

  • Expressions: <% = expression%> The expression is evaluated and printed out the document.
  • Scriptlet <% code%> The code is inserted into the servlet service method.
  • Statements: <%! 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>

Solution 4

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

Share:
102,064
Baba
Author by

Baba

Updated on July 05, 2022

Comments

  • Baba
    Baba almost 2 years

    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