3
function ShowMessage() {
    var url = '/primes/PrimesServlet';
    var result = CQ.HTTP.eval(url);
    var i = Number(document.getElementById("input").value);
    var str = document.getElementById("test");
    // if(result.checkPrimes(i)) // Want to call here
    str.innerHTML = i + " là số nguyên tố";
    // else str.innerHTML = i + " là hợp số";
    document.getElementById("output").style.display="block";
}

public class PrimesServlet extends HttpServlet {
    public boolean checkPrimes(long n) {
        boolean _return;
        MillerRabin obj = new MillerRabin();
        _return = obj.checkPrimes(n);
        return _return;
    }
 }

I want to call the method checkPrimes(long n) from my function ShowMessage(), but I can't.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
anhhtbk
  • 89
  • 2
  • 12
  • You may want to read up on http://en.wikipedia.org/wiki/Ajax_(programming) – aquaraga Oct 28 '13 at 10:34
  • You'll need to call it via an AJAX request, since your java code is running on your server, and your JavaScript is executed client-side. Expose the Java method as a web service, make an [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from your JS, and make the return type of your Java method something which you can serialize, and parse on the front-end. – nbrooks Oct 28 '13 at 10:35

4 Answers4

2

I want to call method checkPrimes(long n) from function javascript "ShowMessage()" but I can't.

Yes you cannot.

Java plays on server side and javascript plays on client side.

Java needs compiled code and Javascript is just a scripting language interpreted by the browser.

What you need is just make a request to server.

That request may be

  1. form submission

  2. Ajax

Besides all with javascript you can check Prime like.

function isPrime1(n) {
    if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false; 
    var m=Math.sqrt(n);
    for (var i=2;i<=m;i++) if (n%i==0) return false;
    return true;
}

Just found here.

If you want to make a request with JavaScript, Ajax is your friend.

A great example :How to use Servlets and Ajax?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

you can not call methods using javascript because javascript runs in client side and javacodes lies in server side.

to call a method checkPrimes you can use ajax.

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

You could ether use AJAX-Calls or write a controller and call it via a request from the JavaScript.

The proble is (as the others said), that javascript runs in the browser of the client and your businesslogic runs on the webserver.

With AJAX you can trigger the server to execute code.

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
0

You can't call like that. You should do operation on doGet or doPost methods than write the result on PrintWriter.write method.

public class PrimesServlet extends HttpServlet {

 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                           throws ServletException, IOException{
 boolean _return;
 MillerRabin obj = new MillerRabin();
 _return = obj.checkPrimes(n);
 resp.getWriter().write(_return);

 }
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • If I use "resp.getWriter().write(_return);", browser will open new website. I want to keep my old website. p.s: My English is bad. – anhhtbk Oct 28 '13 at 11:12