java method call from javascript in jsp page - Stack Overflow

I am new to JSP,I have method that contains loop and want to display value <%!private void getDemo

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?

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?

Share Improve this question asked May 28, 2013 at 5:13 Surendra JnawaliSurendra Jnawali 3,2305 gold badges30 silver badges45 bronze badges 5
  • NO. You cannot call script code inside java.. Java is a server side. script code are client side – Dineshkani Commented May 28, 2013 at 5:15
  • but the last value display here... – Surendra Jnawali Commented May 28, 2013 at 5:18
  • In browser you don't see any JSP or java code. What you see is just a plain HTML with Javascript and/or CSS. All these things can just be the output of the JSP/servlet code (not JSP or java itself) which runs on the server. – me_digvijay Commented May 28, 2013 at 5:22
  • 1 @SJnawali when your code runs all the jsp code are run first and then it executes the script code. Read the JSP life cycle tutorialspoint./jsp/jsp_life_cycle.htm and also read about jsp tags dont use jsp declaration tag in all your code – Dineshkani Commented May 28, 2013 at 5:26
  • @Dineshkani: yes you are correct...hmm thanks for information. – Surendra Jnawali Commented May 28, 2013 at 5:28
Add a ment  | 

1 Answer 1

Reset to default 1

Your Javascript runs client side in the browser so will not normally interact with your Java code running server side

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745262897a4619291.html

相关推荐

  • java method call from javascript in jsp page - Stack Overflow

    I am new to JSP,I have method that contains loop and want to display value <%!private void getDemo

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信