user4695032
user4695032

Reputation: 15

Calling Java method from HTML file

I'm building a web application, and the following Java method will be called dynamically several times when the application's web page is opened in a browser:

Java:

public String getProductImgURL (int i) // image at index i
{
    return products_to_display.get(i).getImage();
}

I want to be able to call this function in for loop in the HTML file, I tried making the calls using JavaScript's document.write in the following way, however it didn't work.

Part of the JavaScript inside HTML file:

   for(var i=0;i<len;i++)
   {
        document.write(
        "<img src=\"@model.getProductImgURL(i)\">"+
        "<br>"
        );
   }

It doesn't work, the weird thing is that when I pass a constant number to the function above in JavaScript, it works just fine!

I'm new to this, can anyone help me find a way calling the Java function above in the HTML file using JavaScript?

Thanks

Upvotes: 0

Views: 476

Answers (1)

mkoryak
mkoryak

Reputation: 57918

you cannot call a serverside method via clientside code. they are not the same thing, they dont live on the same machines etc..

you will need to make an http call to a server that will call the java method.

Upvotes: 1

Related Questions