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.

2
  • You may want to read up on en.wikipedia.org/wiki/Ajax_(programming) Commented Oct 28, 2013 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 from your JS, and make the return type of your Java method something which you can serialize, and parse on the front-end. Commented Oct 28, 2013 at 10:35

4 Answers 4

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I don't need that.CheckPrime only is exampe. I want to use sevlet in javascript.
So Ajax is your friend.See here.stackoverflow.com/questions/4112686/…
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.

Comments

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.

Comments

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);

 }
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.