-3

I want to call a java method getMessage() from a jar file whenever I click button on my webpage. I want to know how to achieve this.

My html file : index.html

   <!doctype html>
   <html>
   <head>
   <meta charset="utf-8"/>
   <script src="jquery-1.11.1.min.js"></script> 
   <script language="javascript">   

      function openMsg()
     {        
        var request = $.ajax({           // Here I am just calling jar file but I want the code to call method getMessage(); 
                   url: "Example.jar",
                   type:"GET",
                   dataType:"html"
                         });     

        request.done(function(msg){
                $("print").html(msg);
                         });

        request.fail(function(jqXHR, textStatus){
                alert("Request failed :",textStatus);
                         });                                    

     }       

   </script> 
   </head>

  <body>
   <button onclick="openMsg();">Click to open message</button>
   <p id="print"></p>
</body>
</html>

My java file : Example.java

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JApplet;

    public class Example extends JApplet
  {

        public void start() 
      {

      }

        public void init()  
      {
         setBackground(Color.blue);
      }

        public void paint(Graphics g)   
      {
         g.drawString("Hello World !", 100, 100);
      }

         public void getMessage()
      {
          System.out.println("Good Morning..!!!");
      }

  }

Please someone tell me how can I call getMessage() method from Example.jar

Mohit Gamot
  • 19
  • 1
  • 7
  • And where would you like that `System.out.println` to appear? – Thilo Oct 18 '16 at 15:26
  • the HTTP GET request arrives at a webserver. That webserver not has to call your java code and that is pretty broad. Also your code extends ``JApplet``, which is something totally unrelated. – f1sh Oct 18 '16 at 15:27
  • You need to install some server-side code that responds to HTTP requests by running your Java class. – Thilo Oct 18 '16 at 15:27
  • Are you expeciting the Example.java to be on the user's machine? (client-side)? – freedomn-m Oct 18 '16 at 15:29
  • 2
    Please don't repost the same question again. Here is the original question asked some hours ago: http://stackoverflow.com/questions/40104912/how-to-call-java-methods-from-javascript-using-ajax – vanje Oct 18 '16 at 15:34
  • @Thilo What I actually want to do is I have a java code which when fed with a string, encrypts it and returns it back to me. So now as some of the modern browsers have withdrawn their support for java , I am confused where should I place my jar file ? Should I include it in webpage(client-side) or on server-side? I am thinking this with respect to security . I don't want the code that encrypts to be visible. Actually I wanted to keep the jar on client side but as applet is deprecated and since its a bad practice to call java methods from javascript .I am still thinking .Plz suggest something. – Mohit Gamot Oct 18 '16 at 15:45
  • You can probably implement the encryption in Javascript. – Thilo Oct 18 '16 at 15:46
  • @Thilo do you mean literally in javascript or embedding java in javascript? Because if I implement it in javascript ..the whole code would be easily visible to the public if they do inspect element. – Mohit Gamot Oct 18 '16 at 15:56
  • Encryption algorithms should be open to inspection. And applets are also downloaded to the client and can be taken apart there, so it's not really a difference. Anyway, if you want to move the code to your server, you need to set up something like a servlet. – Thilo Oct 18 '16 at 23:19

1 Answers1

1

you should create an http servlet to intercept the request (java dows not provide the connectivity). Try to see here

Community
  • 1
  • 1
Gio
  • 33
  • 6