简体   繁体   中英

java method call from javascript in jsp page

I am new to JSP,
I have method that contains loop and want to display value

<%!
private void getDemo() {
    for(int i=0; i<10; i++) {
        setName("Val: " + i); %>
        <script language="javascript">
            var name = "<%= getName()%>";
            console.log(name);
        </script>
    <%!}
}%>
<%!
String name;
private void setName(String name) {
    this.name = name;
}
private String getName() {
    return this.name;
}
%>

Call

<script type = "text/javascript">
 window.onload=function(){<%getDemo();%>};
</script>

Problem
But when I see the console, It only display Val: 9
How can I achieve all value? I need Script code inside Java Code. Is it Possible?

您的 Javascript 在浏览器中运行客户端,因此通常不会与运行服务器端的 Java 代码交互

Though I am not sure if the following is fully supported in all browsers, but you may wish to try as you are new to and wish to explore. this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM