如何在函数javascript中调用方法javaservlet?

时间:2022-03-15 16:57:58
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.

我想从我的函数ShowMessage()调用方法checkPrimes(long n),但我不能。

4 个解决方案

#1


2  

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

我想从函数javascript“ShowMessage()”调用方法checkPrimes(long n),但我不能。

Yes you cannot.

是的你不能。

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

Java在服务器端播放,javascript在客户端播放。

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

Java需要编译代码,Javascript只是浏览器解释的脚本语言。

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.

除了所有的JavaScript,你可以检查Prime喜欢。

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.

如果你想用JavaScript发出请求,Ajax就是你的朋友。

A great example :How to use Servlets and Ajax?

一个很好的例子:如何使用Servlets和Ajax?

#2


0  

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

你不能使用javascript调用方法,因为javascript在客户端运行而javacodes位于服务器端。

to call a method checkPrimes you can use ajax.

调用方法checkPrimes你可以使用ajax。

#3


0  

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

您可以使用AJAX-Calls或编写控制器,并通过JavaScript的请求调用它。

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

问题是(正如其他人所说),javascript在客户端的浏览器中运行,而您的businesslogic在Web服务器上运行。

With AJAX you can trigger the server to execute code.

使用AJAX,您可以触发服务器执行代码。

#4


0  

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

你不能那样打电话。您应该对doGet或doPost方法执行操作,而不是在PrintWriter.write方法上写入结果。

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


2  

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

我想从函数javascript“ShowMessage()”调用方法checkPrimes(long n),但我不能。

Yes you cannot.

是的你不能。

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

Java在服务器端播放,javascript在客户端播放。

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

Java需要编译代码,Javascript只是浏览器解释的脚本语言。

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.

除了所有的JavaScript,你可以检查Prime喜欢。

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.

如果你想用JavaScript发出请求,Ajax就是你的朋友。

A great example :How to use Servlets and Ajax?

一个很好的例子:如何使用Servlets和Ajax?

#2


0  

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

你不能使用javascript调用方法,因为javascript在客户端运行而javacodes位于服务器端。

to call a method checkPrimes you can use ajax.

调用方法checkPrimes你可以使用ajax。

#3


0  

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

您可以使用AJAX-Calls或编写控制器,并通过JavaScript的请求调用它。

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

问题是(正如其他人所说),javascript在客户端的浏览器中运行,而您的businesslogic在Web服务器上运行。

With AJAX you can trigger the server to execute code.

使用AJAX,您可以触发服务器执行代码。

#4


0  

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

你不能那样打电话。您应该对doGet或doPost方法执行操作,而不是在PrintWriter.write方法上写入结果。

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

 }
}