0

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

1 Answer 1

1

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.

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

4 Comments

Is it possible to get clearer directions/example please?
no, your question is too broad - I don't have time to explain how the internet works
Before I posted the question I made some research on the internet. From stuff I read, I think the solution can be AJAX, however I didn't really understand how I can use it. Especially the part of combining AJAX and where the Java method call. That is an example of what I mean by "directions" - I'm not asking from anyone to explain how the WHOLE internet works.
sorry, but your question is still to broad. you want me to explain how client server applications work? you can google that.

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.