admin管理员组文章数量:1418437
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?
- 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
1 Answer
Reset to default 1Your Javascript runs client side in the browser so will not normally interact with your Java code running server side
本文标签: java method call from javascript in jsp pageStack Overflow
版权声明:本文标题:java method call from javascript in jsp page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745262897a2650436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论